python csv writer is adding quotes when not needed

2024/4/15 0:30:01

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)
Answer

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'
https://en.xdnf.cn/q/73314.html

Related Q&A

How to install google.cloud automl_v1beta1 for python using anaconda?

Google Cloud AutoML has python example code for detection, but I have error when importing these modulesfrom google.cloud import automl_v1beta1 from google.cloud.automl_v1beta1.proto import service_pb2…

Python3.8 - FastAPI and Serverless (AWS Lambda) - Unable to process files sent to api endpoint

Ive been using FastAPI with Serverless through AWS Lambda functions for a couple of months now and it works perfectly.Im creating a new api endpoint which requires one file to be sent.It works perfectl…

How to create a function for recursively generating iterating functions

I currently have a bit of Python code that looks like this:for set_k in data:for tup_j in set_k:for tup_l in tup_j:The problem is, Id like the number of nested for statements to differ based on user in…

How to get molecular weight of a compound in python?

User inputs a formula, for example: C12H2COOHWe have to calculate its molecular weight given that C = 12.01, H = 1.008 and O = 16. We were told to be careful of elements with double digits after it and…

How to install Numpy and Pandas for AWS Lambdas?

Problem: I wanted to use Numpy and Pandas in my AWS lambda function. I am working on Windows 10 with PyCharm. My function compiles and works fine on local machine, however, as soon as package it up and…

vim highlighting everything in red

I added a print line to a python script while the script was executing, and now all the text is highlighted in red when I open the file. Opening and closing the file doesnt get rid of it. Opening a sec…

Using python modules in node.js

Is it possible to create a glue that makes it possible for python modules (more specifically, library bindings) to be used in node.js? Some of the data structures can be directly mapped to V8 objects …

Using PHP to call Virtualenv’ed Python Script

Last night I spent 5.5 hours trying make PHP execute and receive the output of Virtualenv’ed Python script. Nothing worked; except for scripts that were not Virtualenv’ed.What I am trying to do:I am …

How do I find my program name?

ProblemI am unable to write to a different log than the default one using syslog. I am unsure if maybe my app name is wrong in my configuration. Do "program name" and "process name"…

Is it possible to automate the execution of a Python script using Microsoft Flow?

I want to execute a snippet of python code based on some trigger using Microsoft-Flow. Is there a way to do this? Basically I am exploring on Powerapps and Microsoft-Flow. I have data in powerapp, I c…