1

Until very recently this piece of code worked. I added a function for prompt the user for a file name and extension, then that didn't work so I resorted back to this version. Now when I try to run it, I get this:

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

I'm nor completely sure what the error means, and after researching I could only find python object not iterable error in function and python3 TypeError: 'function' object is not iterable which do not solve my problem.

Below is the code giving me the error:

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

If needed, I will post the full code in a later edit. Any detailed answer explaining what I need to change would be very helpful, I want to learn the language and what I am doing wrong so (hopefully) later I won't make the same mistake.

Edit: Here is the entirety of my code

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()
Community
  • 1
  • 1
TobyTobyo
  • 405
  • 2
  • 6
  • 20

2 Answers2

8

Shot in the dark:

intermediate_count is a helper function you made that returns an iterable.

Solution (if my guess was correct), you need to call the function (), otherwise as written you are passing the function object itself, which is what the error is telling you

writer(final_counts(intermediate_count(location, filetype)))
                                      ^
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
1

Change your main function to read like so:

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

Alternatively, you could write it this way:

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

And then change this:

def intermediate_count(location, filetype):

To this instead:

def intermediate_count():
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
  • Thanks for the response but Cory's works quite well. I need to keep the location and filetype outside of the intermediate count function when I start prompting for user input (continual repetition of input rejection until a "good" value is entered). – TobyTobyo Jul 10 '15 at 20:25