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)
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.
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.
In Python, while reading a CSV using the CSV module you can skip the first line using next() method.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With