Fiach ONeill
Fiach ONeill

Reputation: 155

For loop is skipped when iterating through CSV

So I have a program that reads a CSV file and performs some computations with the data before outputting it to a separate file. It was working fine yesterday, but when I came back it the program terminates without ever calling the for loop that iterates through each line of the CSV. No error is given. Does anyone know the reason for this?

Below is the function.

def my_map(my_input_stream, my_output_stream, my_mapper_input_parameters):
    #files = glob.glob(my_input_stream)
    #f = open(my_input_stream,"w")
    out = codecs.open(my_output_stream,"w")
    with open(my_input_stream) as csv_file:
        csv_reader = csv.reader(csv_file)
        for line in csv_reader:
            stop_station = line[7]
            start_station = line[3]
            print(start_station)
            out.write(f"{start_station}\t(1,0)\n")
            print("Writing to file..")
            out.write(f"{stop_station}\t(0,1)\n")
    out.close()

Everything works fine apart from the for loop.

Upvotes: 0

Views: 54

Answers (1)

v0rtex20k
v0rtex20k

Reputation: 1121

One of the lines you commented out is

f = open(my_input_stream,"w")

If you ever ran this, then my_input_stream would have been opened in "write" mode. This may have overwritten the data in the file and left you with a blank file, which would explain why the for loop is "skipped" - there's no data to iterate over.

Can you verify that my_input_stream still has actual data in it?

Upvotes: 1

Related Questions