直到最近这段代码才起作用。我添加了一个用于提示用户输入文件名和扩展名的功能,然后这个功能不起作用,所以我回到了这个版本。现在,当我尝试运行它,我得到这个:TypeError:'函数'对象不可迭代
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 function和python3 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()
'final_counts'被传递的功能,而不是一个可迭代。你可以显示调用'final_counts'的代码吗? – TigerhawkT3
@ TigerhawkT3我会在一秒内发布它。 – TobyTobyo