TypeError:'功能'对象不可迭代

时间:2015-07-10 16:51:47

标签: python python-2.7

直到最近这段代码才有效。我添加了一个函数来提示用户输入文件名和扩展名,然后那个没有用,所以我又回到了这个版本。现在,当我尝试运行它时,我明白了:

Traceback (most recent call last):
  File "C:/Code/Samples/Dates/2015-06-07/Large-Scale Data Parsing/Parser/Single_File_Multistep_Counter.py", line 53, in <module>
main()
  File "C:/Code/Samples/Dates/2015-06-07/Large-Scale Data Parsing/Parser/Single_File_Multistep_Counter.py", line 51, in main
writer(final_counts(intermediate_count))
  File "C:/Code/Samples/Dates/2015-06-07/Large-Scale Data Parsing/Parser/Single_File_Multistep_Counter.py", line 31, in final_counts
for file_path in intermediate_file_list:
TypeError: 'function' object is not iterable

我也不完全确定错误的含义,经过研究后,我发现python object not iterable error in functionpython3 TypeError: 'function' object is not iterable无法解决我的问题。

以下是给我错误的代码:

def final_counts(intermediate_file_list):
    date_list = {}
    for file_path in intermediate_file_list:
        with open(file_path, "r") as f:
            for line in f:
                tockens = line.split(",")
                if tockens[0] in date_list:
                    date_list[tockens[0]] = date_list[tockens[0]] + tockens[1]
                else:
                    date_list[tockens[0]] = tockens[1]
    return date_list

如果需要,我会在稍后的编辑中发布完整代码。解释我需要改变什么的任何详细答案都会非常有帮助,我想学习语言和我做错了所以(希望)以后我不会犯同样的错误。

修改:以下是我的全部代码

import os, glob, csv

location = "C:/Code/Samples/Dates/2015-06-07/Large-Scale Data Parsing/Data Files"
columnname = "smcn"
timecolumn = ""
filetype = ".processed"

def intermediate_count(location, filetype):
    intermediate_file_list = []
    for file_path in list(glob.glob(os.path.join(location, "*" + filetype))):
        date_list = {}
        print file_path
        with open(file_path, 'r') as f:
            for line in f:
                tockens = line.split(",")
                key = tockens[9] + "/" + tockens[15][:-4] #replace col_positon to 9 if necessary
                if key in date_list:
                    date_list[key] = date_list[key] + 1
                else:
                    date_list[key] = 1
        with open(file_path + ".count", "w") as csv:
            for item in date_list:
                csv.write(item + "," + str(date_list[item]) + "\n")
        intermediate_file_list.append(file_path + ".count")
    return intermediate_file_list

def final_counts(intermediate_file_list):
    date_list = {}
    for file_path in intermediate_file_list:
        with open(file_path, "r") as f:
            for line in f:
                tockens = line.split(",")
                if tockens[0] in date_list:
                    date_list[tockens[0]] = date_list[tockens[0]] + tockens[1]
                else:
                    date_list[tockens[0]] = tockens[1]
    return date_list

def writer(date_list):
    directory = location + "/" + "Total"
    if not os.path.exists(directory):
        os.makedirs(directory)
    with open(directory + "/" + "Total.processed", "w") as f: # Lots of commas!
        writer = csv.writer(f, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        for key, value in date_list.items():
            writer.writerow(str([key]) + str(value))

def main():
    writer(final_counts(intermediate_count))

main()

2 个答案:

答案 0 :(得分:6)

在黑暗中拍摄:

intermediate_count是一个帮助函数,它返回一个可迭代的函数。

解决方案(如果我的猜测是正确的),你需要调用函数(),否则你写的是传递函数对象本身,这就是错误告诉你的事情

writer(final_counts(intermediate_count(location, filetype)))
                                      ^

答案 1 :(得分:1)

main功能更改为如下所示:

def main():
    writer(final_counts(intermediate_count(location, filetype))

或者,您可以这样写:

def main():
    writer(final_counts(intermediate_count())

然后改变这个:

def intermediate_count(location, filetype):

改为:

def intermediate_count():