Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python csv writer is adding quotes when not needed

Tags:

python

json

csv

I am having issues with writing json objects to a file using csv writer, the json objects seem to have multiple double quotes around them thus causing the json objects to become invalid, here is the result:

"{""user.CustomAttribute.ISOLanguageCode"": ""en"", ""user.Email"": ""[email protected]""

what I want is

{"user.CustomAttribute.ISOLanguageCode": "en", "user.Email"": "[email protected]"}

here is how I open the file, perhaps there is an argument I can pass to prevent this from happening?

file = csv.writer(open(localResultPath + ".txt",'ab'),delimiter = '|')

here is how I write to the file, the last append adds the json as a string

list.append(pk)
list.append(email)
list.append(json)
file.writerow(list)
like image 730
godzilla Avatar asked Mar 18 '14 16:03

godzilla


2 Answers

Switch off auto-quoting with quoting=csv.QUOTE_NONE, and set quotechar to the empty string:

file = csv.writer(open(localResultPath + ".txt",'ab'), 
                  delimiter='|', quoting=csv.QUOTE_NONE, quotechar='')

Even with csv.QUOTE_NONE the csv.writer() will still want to quote the quotechar if left set to anything but an empty string, if present in the value. The default quote character is " and JSON values are full of those.

Demo:

>>> from cStringIO import StringIO
>>> import csv
>>> f = StringIO()
>>> writer = csv.writer(f, delimiter='|', quoting=csv.QUOTE_NONE, quotechar='')
>>> writer.writerow(['{"user.CustomAttribute.ISOLanguageCode": "en"}'])
>>> f.getvalue()
'{"user.CustomAttribute.ISOLanguageCode": "en"}\r\n'
like image 83
Martijn Pieters Avatar answered Oct 24 '22 10:10

Martijn Pieters


You have to change the quotechar to s.th. else than " :

csv.writer(csvfile, delimiter='|',quotechar='&', quoting=csv.QUOTE_MINIMAL)

Make sure that the quote character does not appear in your objects, obviously.

If you switch off quoting you have to be careful when your delimiter appears in the JSON-Object as well. Better way is using the json module and its functions json.dump() and json.load() for writing and reading the objects

like image 30
MaSp Avatar answered Oct 24 '22 11:10

MaSp