I'm attempting to write a list to a csv, however when I do so I get wrapper quotes around my field values:
number1,number2
"1234,2345"
"1235.7890"
"2345.5687"
Using this code:
with open('C:\\temp\\test.csv', 'wb') as out_file:
... csv_writer = csv.writer(out_file, delimiter=',')
... csv_writer.writerow(('number1','number2'))
... for f in myList:
... csv_writer.writerow(f)
After further research, I found that you can remove the writing of quotes by using:
quotechar='', quoting=csv.QUOTE_NONE**
When I apply this to my code I get this error:
Traceback (most recent call last): File "", line 4, in Error: need to escape, but no escapechar set
with open('C:\\temp\\test.csv', 'wb') as out_file:
... csv_writer = csv.writer(out_file, delimiter=',',quotechar='', quoting=csv.QUOTE_NONE)
csv_writer.writerow(('number1','number2'))
... for f in myList:
... csv_writer.writerow(f)
How do I remove these quotes?
Edit
myList looks like:
[['1234,2345'], ['1235,7890'], ['2345,5687']]
what's in your list are not numbers but text, which is even containing the delimiter character. that means to export this as csv it has to be escaped.
you need to convert your data to numbers before you export it to csv if you want it to be written correctly.
edit: on the other hand your data already looks like it consits of comma separated values - why not write it to a file directly without using a csv writer?
来源:https://stackoverflow.com/questions/11372460/csv-remove-field-value-wrap-quotes