List to csv in python with header [closed]
Asked Answered
C

1

5

I have written a script which gives the list like below as an output.

['red', '361', '0']
['blue', '1', '0']
['orange', '77', '0']
['cream', '660', '73']
['ivory', '159', '0']

This list is very huge and I want to write the output contents to a csv with header on top like below.

color | total | fail

Here is how I am trying to achieve it

with open('colour.csv', 'wb') as csvfile:
    header = ['color', 'total', 'fail']
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerow(header)
    for row in output:
        writer.writerow(output)

But the output I am getting is not as expected. The csv is generated and the last row of the list is printed thrice to the csv. I am not able to find why not all other rows are printed or why last row is printed thrice? Here is the sample output I am getting:

color | total | fail
ivory | 159 | 0
ivory | 159 | 0
ivory | 159 | 0

This is what I am expecting the output to be:

color | total | fail
red | 361 | 0 
blue | 1 | 0
orange | 77 | 0
cream | 660 | 73
ivory | 159 | 0

Can anyone please help me with this?

Ceram answered 21/5, 2016 at 18:20 Comment(14)
Shouldn't that be writer.writerow(row)?Casebook
for row in output: writer.writerow(output) looks odd you also have output undefined currently.Hairworm
And where does output come from? That seems pretty critical. It seems it's just the last row instead of all the rows.Casebook
@AlexHall the output is having the list to be written to csv.Ceram
You say that but I'm pretty sure it's just the last row, i.e. a 1D list, instead of all the rows, which would be a 2D list.Casebook
@AlexHall The output prints all the rows of the list and not just the last row. But as a beginner I don't have the idea of 1D or 2D list. It would be great if you explain it more.Ceram
@csvb, you have a simple error. Use row instead of output in the last line. Also specify | as your delimiter instead of ,.Disquietude
@MarkTolonen look at the existing comments, that's been covered twice.Casebook
@alex Hall, per csvb's last comment...he needed more prodding to actually read the comments.Disquietude
@Ceram Please include complete runnable code.Casebook
@MarkTolonen no, the problem he has right now is that output is wrong and he/she doesn't believe it.Casebook
Sorry everyone. But I tried all the option you all suggested. But there are no errors. Here is the running code. This code is calculating the difference of two csv files. pastebin.com/bSsi31Rv. Let me know my mistake. thank you.Ceram
Put output = [] on line 18 and then output.append(row1) instead of output = row1. Now output is a 2D list, i.e. a list of lists. print output after the loop to see.Casebook
Wow. thank you very much. It worked. Now I got the concept of 2D list. I thought we need to define a list only while reading and not while writing. It was a great learning. Thank you for helping me out. It works :)Ceram
U
14

Code -

import csv

arr = [['red', '361', '0'],
      ['blue', '1', '0'],
      ['orange', '77', '0'],
      ['cream', '660', '73'],
      ['ivory', '159', '0']]

with open('output.csv','w') as f:
    writer = csv.writer(f)
    writer.writerow(['color', 'total', 'fail'])
    writer.writerows(arr)
Ululant answered 21/5, 2016 at 18:28 Comment(3)
I have tried this approach before and the way the output is generated is different from what I want. The letters of the words in the list gets printed to individual column of csv like below: r |e | d | 3|6|1Ceram
@Ceram did you try this code? The result you got was from using writer.writerow instead of writer.writerows.Disquietude
@Ceram - this solution is correct, although it may be a good idea to pass lineterminator='\n' to csv.writer() to avoid the newlines. If you're getting funny output, along the lines of what you've just mentioned, you may be using some sort of custom settings on your CSV reader. If you're looking for some custom format, try tinkering with the delimiter and quotechar parameters,Libelee

© 2022 - 2024 — McMap. All rights reserved.