-1

当我测试我的代码时:

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

所以,我只是字体明白,错误在哪里。

谢谢!

4

1 回答 1

3

你只是想要for line in myfile:file可以直接迭代对象(一次产生 1 行)。但是,文件对象不支持调用(例如myfile(),因为file.__call__未实现而未实现)。

于 2012-11-01T15:45:04.080 回答