6

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']]
tshepang
  • 12,111
  • 21
  • 91
  • 136
artwork21
  • 330
  • 12
  • 29
  • What program do you use to read the actual CSV file? Do the quotes show up in a text editor? – Blender Jul 07 '12 at 05:18
  • @Blender, I use arcmap, the values are going under the number1 field only. – artwork21 Jul 07 '12 at 05:40
  • You are getting quotes (or a grumble about no escapechar) because you have something in `myList` that needs quoting or escaping. Show us the first 3 rows in `myList` as obtained by `print myList[:3]` and copy/paste the output into an edit of your question. – John Machin Jul 07 '12 at 05:42

1 Answers1

7

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?

mata
  • 67,110
  • 10
  • 163
  • 162