2015-07-10 75 views
1

直到最近这段代码才起作用。我添加了一个用于提示用户输入文件名和扩展名的功能,然后这个功能不起作用,所以我回到了这个版本。现在,当我尝试运行它,我得到这个: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 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

'final_counts'被传递的功能,而不是一个可迭代。你可以显示调用'final_counts'的代码吗? – TigerhawkT3

+0

@ TigerhawkT3我会在一秒内发布它。 – TobyTobyo

回答

6

瞎猜:

intermediate_count是你做的是返回一个可迭代的辅助功能。

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

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

宾果!我需要等待几分钟才能接受您的答案,但谢谢! – TobyTobyo

1

更改main函数读取,像这样:

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

或者,你可以WRI忒这样说:

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

,然后改变这一点:

def intermediate_count(location, filetype): 

为了这个:

def intermediate_count(): 
+0

感谢您的回应,但Cory的作品相当出色。当我开始提示用户输入时(不断重复输入拒绝,直到输入“好”值),我需要将位置和文件类型保持在中间计数函数之外。 – TobyTobyo

相关问题