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)
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'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With