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?
writer.writerow(row)
? – Casebookfor row in output: writer.writerow(output)
looks odd you also haveoutput
undefined currently. – Hairwormoutput
come from? That seems pretty critical. It seems it's just the last row instead of all the rows. – Casebookoutput
is having the list to be written to csv. – Ceramrow
instead ofoutput
in the last line. Also specify|
as your delimiter instead of,
. – Disquietudeoutput
is wrong and he/she doesn't believe it. – Casebookoutput = []
on line 18 and thenoutput.append(row1)
instead ofoutput = row1
. Nowoutput
is a 2D list, i.e. a list of lists.print output
after the loop to see. – Casebook