Reputation: 147
I have created a dictionary called students, so for example when I say print students[Student00, Student00] I get ('111111100', 72, 40)} and so on for the other student names
I want to output the dictionary into a csv file with each entry in its own cell in the csv so Student00, Student00 would be in the first cell, then 111111100 in the next cell, 72 in the next cell and then 40 in the last cell. I have tried the below code and it did not work. It printed the key in the first cell and then in the second cell the values but I want each value in its own cell. How can I get this to happen?
with open('ur file.csv','wb') as out:
for key, val in students.items():
csv_out=csv.writer(out)
csv_out.writerow([key, val])
for row in students:
csv_out.writerow(row)
Upvotes: 0
Views: 59
Reputation: 16941
The problem is this line:
csv_out.writerow([key, val])
This line says, write me out 2 cells, with key
in the first cell and val
in the second. If you want writerow
to write 4 cells then you have to provide it a list of length 4. From your description, val
is a 3-tuple. Try this:
csv_out.writerow([key] + list(val))
Upvotes: 2
Reputation: 8888
with open('ur file.csv','wb') as out:
csv_out=csv.writer(out)
# then do for loop and write stuff...
Upvotes: 0