简体   繁体   中英

For loop not iterating through CSV with Python

Essentially what I am trying to do here is place all data from a CSV file into a dictionary in a nested format so I can just dump the json after I need it. I need the json formatted like [{"name":"Ollie","email":"josh@apple.com","phone":"0444444444","sizes":[],"taskAmount":1,"singleCheckout":true,"billingDifferent":false,"favorite":true

I have a for loop in place that should take all csv values and place them in the dictionary in my proper format.

I have tried looking at my loops and it checks out so I can't see why it doesn't work. Also, in the if statemnt if I go new_data_dict[row[0]] in place of new_data_dict it runs properly but doesnt come out in my desired format.

My code is here

profiles = []
new_data_dict = {}
with open("test.csv", 'r') as data_file:
    reader = csv.reader(data_file)
    for row in reader:
        if row[21] == 'null':
            new_data_dict = {"name":row[0],"email":row[1],"phone":row[15],"sizes":[],"taskAmount":1,"singleCheckout":True,"billingDifferent":False,"favorite":False,"card":{"number":row[5],"expiryMonth":row[7],"expiryYear":row[8],"cvv":row[6]},}
        else:
            new_data_dict = {"name":row[0],"email":row[1],"phone":row[15],"sizes":[],"taskAmount":1,"singleCheckout":True,"billingDifferent":True,"favorite":False,"card":{"number":row[5],"expiryMonth":row[7],"expiryYear":row[8],"cvv":row[6]},"delivery":{"firstName":row[20],"lastName":row[21], }}

print(new_data_dict)

My input file contains some sensitive information so I can't really show it but the code gives you an idea of what is in the rows.

What I am getting out is one iteration of the loop which also happens to be the last row of data in the csv, as if its starting backwards.

What went wrong?

When you are reading rows from the csv file and assigning them into new_data_dict in a for loop, you are repeatedly overwriting the rows that were already stored in it.

How to fix it?

You can either put all of them into a list or another dictionary, for example:

all_records = []

for row in reader: 
    row_data_dict = {"name":row[0],"email":row[1],"phone":row[15],"sizes":[]...}

    all_records.append(row_data_dict)

Then the output would look like what you described as the desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM