CSV removes field wrap fields

I am trying to write a list in csv, however, when I do this, I get shell quotes around the values ​​of my field:

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 delete the quotation mark using:

quotechar = '', quoting = csv.QUOTE_NONE **

When I apply this to my code, I get this error:

Traceback (last last call): File "", line 4, in Error: need to run, 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 to remove these quotes?

Edit

myList looks like this:

  [['1234,2345'], ['1235,7890'], ['2345,5687']] 
+6
source share
1 answer

that your list doesn’t have numbers, but text that even contains a separator character. this means that for export it is like csv it must be escaped.

you need to convert your data to numbers before exporting it to csv if you want it to be spelled correctly.

edit: on the other hand, your data already looks as if it consists of values ​​separated by commas - why not write them to a file directly without using csv writer?

+7
source

Source: https://habr.com/ru/post/919919/


All Articles