[英]Getting the TypeError: 'function' object is not iterable
[英]TypeError: 'function' object is not iterable
直到最近,這段代碼才起作用。 我添加了一個功能來提示用戶輸入文件名和擴展名,然后該功能不起作用,因此我重新使用此版本。 現在,當我嘗試運行它時,我得到了:
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對象在函數中不是可迭代的錯誤,而python3 TypeError:'function'對象不是可迭代的 ,不能解決我的問題。
以下是給我錯誤的代碼:
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()
在黑暗中拍攝:
intermediate_count
是您制作的一個幫助程序函數,它返回一個可迭代的函數。
解決方案(如果我的猜測是正確的),則需要調用function ()
,否則如您所寫,您正在傳遞函數對象本身,這就是錯誤告訴您的內容
writer(final_counts(intermediate_count(location, filetype)))
^
將您的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():
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.