Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a list as a .csv file with python with new lines?

I would like to save a python list in a .csv file, for example I have a list like this:

['hello','how','are','you'] 

I would like to save it like this:

colummn, hello, how, are, you, 

I tried the following:

myfile = open('/Users/user/Projects/list.csv', 'wb') wr = csv.writer(myfile, quoting=csv.QUOTE_ALL,'\n') wr.writerow(pos_score) 
like image 928
skwoi Avatar asked Mar 27 '15 21:03

skwoi


People also ask

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

To convert the list to csv, we need to convert from list to dataframe and then use the to_csv() function to convert dataframe to a csv file. In this example, we have first imported pandas library and then define the four lists and map it with its column using a dictionary.

Can I save list to csv Python?

The mode “a” is used to append the file, and writer = csv. writer(f) is used to write all the data from the list to a CSV file. The writer. writerow is used to write each row of the list to a CSV file.

How do I skip the first line of a CSV file in Python?

In Python, while reading a CSV using the CSV module you can skip the first line using next() method.


1 Answers

use pandas to_csv (http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.to_csv.html)

>>> import pandas as pd >>> df = pd.DataFrame(some_list, columns=["colummn"]) >>> df.to_csv('list.csv', index=False) 
like image 170
grechut Avatar answered Oct 12 '22 22:10

grechut