![](/img/trans.png)
[英]I am getting an error : 'type' object has no attribute '__getitem__'
[英]i am getting an error of 'file' object has no attribute '__getitem__' when i run the following code in Boa constructor GUI base python program
當我在Boa構造函數GUI基礎python程序中運行以下代碼時。
def OnGraphitButton(self, event):
VDWAALS=[]
file=open("/home/fareeha/Desktop/GraphMDresearch_paper/Sourcecodes/b.csv",'r')
line=file.readlines()
parts = file[-1].split(",")[0]
frame_number = parts
for a in line:
if 'DELTA Energy Terms' in a:
tick=2
print ("The frame is: ", frame_number, re.findall("\d+", Bframe_number))
while tick<int(frame_number)+1:
print a, "##", frame_number,tick
VDWAALS.append(line[line.index(a)+tick].split(',')[1])
您正在將文件內容讀入成為字符串列表的line
中,但是隨后嘗試從file
對象而不是line
檢索最后line
,從而導致錯誤。
已修復此錯誤的代碼如下所示:
def OnGraphitButton(self, event):
VDWAALS=[]
file=open("/home/fareeha/Desktop/GraphMDresearch_paper/Sourcecodes/b.csv",'r')
line=file.readlines()
parts = line[-1].split(",")[0] # <<< FIXED LINE
frame_number = parts
for a in line:
if 'DELTA Energy Terms' in a:
tick=2
print ("The frame is: ", frame_number, re.findall("\d+", Bframe_number))
while tick<int(frame_number)+1:
print a, "##", frame_number,tick
VDWAALS.append(line[line.index(a)+tick].split(',')[1])
請確保將來發布您的實際錯誤回溯,而不是試圖描述它們的說法。 無需相關的追溯,就可以輕松地在他人的代碼中檢測到最瑣碎的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.