I have a list that I want to export to a CSV
file in a single row.
import csv
lst = [7.587069e-05, -5.956967e-08]
f = open('myresults.csv', 'w', newline='')
writer = csv.writer(f)
writer.writerow(lst)
f.close()
The problem is the output seen to be perceived as a single column like the one shown below:
Obviously, I need my data to be separated into multiple columns.
Any idea on what the problem is?
CodePudding user response:
The problem is not in your script but in Microsoft Excel: depending on your local country, the default separator can be a comma ,
or a semicolon ;
.
You have to add the comma option as said on this page.
CodePudding user response:
l = [7.587069e-05,-5.956967e-08]
with open(r'C:\directory\myresults.csv', 'w', newline='') as f:
writer = csv.writer(f, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(l)
https://docs.python.org/3/library/csv.html
Basically, create a csv writer and write the first line. Define the delimeter as ',' to seperate the values.