Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing data from a python list to csv row-wise

Tags:

python

csv

People also ask

How do I convert a list into a CSV file in Python?

To convert a list of lists to csv in python, we can use the csv. writer() method along with the csv. writerow() method.

How do I write a data list in a CSV file?

The most common method to write data from a list to CSV file is the writerow() method of writer and DictWriter class. Example 1: Creating a CSV file and writing data row-wise into it using writer class.


Change writer.writerow(data) to writer.writerow([data]).

.writerow takes an iterable and uses each element of that iterable for each column. If you use a list with only one element it will be placed in a single column.

You should also restructure your loop:

for word in header:
    writer.writerow([word])