I have a pipe delimited file I am trying to convert to json using python (2.7). The code reads the text file, converts it based on the delimiter and then converts it to json.
When I run the code, the output in my terminal window is correct. However, when I write to a file the escape slashes \ are being added to the output. And quotation marks with no escapes are being added to the beginning and end of output file.
Based on other answers I've tried setting ensure_ascii to false each time I deal with the json dump. But that's not working.
input.txt:
392|0|9
Code:
import csv
import json
f = open( 'input.txt', 'rU')
reader = csv.DictReader( f, fieldnames = ( "A", "B", "C" ), delimiter='|')
out = json.dumps([ row for row in reader ], ensure_ascii=False)
print out
with open('data.json', 'w') as outfile:
json.dump(out, outfile, ensure_ascii=False)
Output in terminal:
[{"A": "392", "C": "9", "B": "0"}]
Output in data.json:
"[{\"A\": \"392\", \"C\": \"9\", \"B\": \"0\"}]"
I'm new to Python. What can I do to remove the quotation marks (at the start and end) and the slashes from the .json file?
In JSON the only characters you must escape are \, ", and control codes.
Furthermore, all double quotes of the actual JSON formatting are escaped with a backslash.
JSON strings do not allow real newlines in its data; it can only have escaped newlines. Snowflake allows escaping the newline character by the use of an additional backslash character.
The json. dump() method (without “s” in “dump”) used to write Python serialized object as JSON formatted data into a file. The json. dumps() method encodes any Python object into JSON formatted String.
You are encoding your data to JSON twice. out
is already JSON encoded, but you encode it again by dumping the JSON string to outfile
.
Just write it out without encoding again:
with open('data.json', 'w') as outfile:
outfile.write(out)
Do remove the ensure_ascii=False
option, as json.dumps()
will then produce unicode values, which would require you to encode them to a suitable codec (read, one of the UTF variants) as you write to the file.
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