Python 类型错误:';str';对象不可调用
我在Python 3.3.2中遇到了以下两个错误:Python 类型错误:';str';对象不可调用,python,Python,我在Python 3.3.2中遇到了以下两个错误: import os path="D:\\Data\\MDF Testing\\MDF 4 -Bangalore\\Bangalore Testing" os.chdir(path) for file in os.listdir("."): if file.endswith(".doc"): print('FileName is ', file) def testcasenames(file): nlines
import os
path="D:\\Data\\MDF Testing\\MDF 4 -Bangalore\\Bangalore Testing"
os.chdir(path)
for file in os.listdir("."):
if file.endswith(".doc"):
print('FileName is ', file)
def testcasenames(file):
nlines = 0
lookup="Test procedures"
procnames=[]
temp=[]
'''Open a doc file and try to get the names of the various test procedures:'''
f = open(file, 'r')
for line in f:
val=int(nlines)+1
if (lookup in line):
val1=int(nlines)
elif(line(int(val))!=" ") and line(int(val1))==lookup):
temp=line.split('.')
procnames.append(temp[1])
else:
continue
return procnames
filename="MDF_Bng_Test.doc"
testcasenames(filename)
当您将
()
与行
一起使用时,它认为行
是一个实际上不是的函数。您实际需要使用的是[]
符号
line[int(val)]!=" " and line[int(val1)]==lookup
问题出在这一行:
elif(line(int(val))!=" ") and line(int(val1))==lookup):
如果您试图为字符串编制索引,Python将使用方括号表示法([]
)来完成它,它将如下所示:
elif(line[int(val)]!=" ") and line[int(val1)]==lookup):
另一个建议是,Python中的括号包装if..else
语句是可选的,没有它们通常代码看起来更好:
elif line[int(val)]!=" " and line[int(val1)]==lookup:
希望这有帮助 如果不需要的话,我不会使用os.chdir。
elif(line[int(val)]!=" ") and line[int(val1)]==lookup):
elif line[int(val)]!=" " and line[int(val1)]==lookup: