Home > Software engineering >  Code not looping through the rest of my file or resetting the counter
Code not looping through the rest of my file or resetting the counter

Time:10-07

Currently trying to loop through a file with addresses in python, but for some reason instead of looping through the entire file - it stops at the first line.

I tried resetting the counter and making it go back to the first if statement but it still only spits out the first address and continues printing the other lines.

If you have any suggestions I would appreciate it!

My code:

starter_file = 'addresses.csv'
output_file = 'new_address.csv'
new_address = ''
line_counter = 0

def save_file(filename, file_mode, content):
    with open (filename, mode=file_mode, encoding='utf-8') as output_file:
        output_file.write(content)

with open(starter_file, mode='r') as map_data:
    for each_line in map_data.readlines():

        if line_counter == 0:
            header = 'street, city, state, zip, coordinates\n'
            save_file(output_file, 'a ', header)
            
        if line_counter == 1:   # street 
            new_address = each_line[1:].strip()     # remove initial ", strip \n from enconding

        if line_counter == 2:   # city, st, zip
            city_state_zip = each_line.strip()
            city_end = len(each_line) - 8
            city = city_state_zip[:city_end]

            state = city_state_zip[city_end:city_end 2]

            zip_code = city_state_zip[-5:]
            
            new_address = new_address   ', '   city   state   ', '   zip_code

        if line_counter == 3:   # coordinates
            new_address = new_address   ', '   each_line.replace(',', ';')
            save_file(output_file, 'a ', new_address)

        if line_counter > 3:
            line_counter == 1

            
        print('#'   str(line_counter)   ' >> ', new_address)

        line_counter  = 1

Output:

#0 >>  
#1 >>  330 Utica Avenue
#2 >>  330 Utica Avenue, Brooklyn, NY , 11213
#3 >>  330 Utica Avenue, Brooklyn, NY , 11213, (40.66668313300005; -73.93135881299997)"

#4 >>  330 Utica Avenue, Brooklyn, NY , 11213, (40.66668313300005; -73.93135881299997)"

#5 >>  330 Utica Avenue, Brooklyn, NY , 11213, (40.66668313300005; -73.93135881299997)"

#6 >>  330 Utica Avenue, Brooklyn, NY , 11213, (40.66668313300005; -73.93135881299997)"

#7 >>  330 Utica Avenue, Brooklyn, NY , 11213, (40.66668313300005; -73.93135881299997)"

#8 >>  330 Utica Avenue, Brooklyn, NY , 11213, (40.66668313300005; -73.93135881299997)"

#9 >>  330 Utica Avenue, Brooklyn, NY , 11213, (40.66668313300005; -73.93135881299997)"

And then it continues looping.

CodePudding user response:

Could it be the following part of your code?

if line_counter > 3:
    line_counter == 1

# ...

line_counter  = 1

When line_counter > 3, it essentially is set to 2 by the start of the next iteration.

Instead, you can remove the if statement altogether and change the last line of your loop to line_counter = (line_counter 1) % 3. If you're unfamiliar with the modulo operator (%), it calculates the remainder. In this case, it creates a "wraparound" effect so that once line_counter reaches 3, it is "reset" to 0.

Edit: Actually, as per Barmar's comment, I realize now that line_counter is only supposed to be zero once - so the code above wouldn't be very helpful as-is. I would suggest using this instead:

line_counter = (line_counter % 3)   1

This way, once line_counter == 3, it wraps around to 1 instead of 0.

  • Related