当我测试我的代码时:
def read_classification_from_file(path, name):
path = add_slash(path) + name
myfile = open(path, "r")
mydict = {}
for line in myfile():
x = line.split(" ")
x[1]=x[1].replace("\n","")
mydict[x[0]]=x[1]
return mydict
def add_slash(path):
if path.endswith('/'): return path
return path + '/'
我收到错误:
Traceback (most recent call last):
File "spamfilter/solution/test_quality_for_corpus.py", line 59, in test_allPredictionsHam
q = self.compute_quality_for_corpus(CORPUS_DIR)
File "/local/ulohy/env/data/4893_1/quality.py", line 9, in compute_quality_for_corpus
truth_dic = utils.read_classification_from_file(corpus_dir, "!truth.txt")
File "/local/ulohy/env/data/4893_1/utils.py", line 5, in read_classification_from_file
for line in myfile():
TypeError: '_io.TextIOWrapper' object is not callable
所以,我只是字体理解,错误在哪里。
谢谢!
答案 0 :(得分:3)
你只想要for line in myfile:
。 file
个对象可以直接迭代(一次产生1行)。但是,文件对象不支持调用(例如myfile()
未实现,因为未实现file.__call__
。