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?

csvb
  • 307
  • 1
  • 5
  • 12
  • 3
    Shouldn't that be `writer.writerow(row)`? – Alex Hall May 21 '16 at 18:22
  • `for row in output: writer.writerow(output)` looks odd you also have `output` undefined currently. – Pythonista May 21 '16 at 18:24
  • 1
    And where does `output` come from? That seems pretty critical. It seems it's just the last row instead of all the rows. – Alex Hall May 21 '16 at 18:24
  • @AlexHall the `output` is having the list to be written to csv. – csvb May 21 '16 at 18:37
  • 1
    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. – Alex Hall May 21 '16 at 18:38
  • @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. – csvb May 21 '16 at 18:49
  • @csvb, you have a simple error. Use `row` instead of `output` in the last line. Also specify `|` as your delimiter instead of `,`. – Mark Tolonen May 21 '16 at 18:51
  • @MarkTolonen look at the existing comments, that's been covered twice. – Alex Hall May 21 '16 at 18:52
  • @alex Hall, per csvb's last comment...he needed more prodding to actually read the comments. – Mark Tolonen May 21 '16 at 18:53
  • @csvb Please include complete runnable code. – Alex Hall May 21 '16 at 18:53
  • @MarkTolonen no, the problem he has right now is that `output` is wrong and he/she doesn't believe it. – Alex Hall May 21 '16 at 18:54
  • 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. http://pastebin.com/bSsi31Rv. Let me know my mistake. thank you. – csvb May 21 '16 at 19:07
  • 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. – Alex Hall May 21 '16 at 19:15
  • 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 :) – csvb May 21 '16 at 19:51

1 Answers1

11

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)
Vedang Mehta
  • 2,044
  • 7
  • 22
  • 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|1` – csvb May 21 '16 at 18:40
  • @csvb did you try this code? The result you got was from using `writer.writerow` instead of `writer.writerows`. – Mark Tolonen May 21 '16 at 18:46
  • 1
    @csvb - 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, – Boa May 21 '16 at 18:48