Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json print output in python different from write output because of escaped characters

Tags:

python

json

csv

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?

like image 951
lucastimmons Avatar asked Sep 27 '13 20:09

lucastimmons


People also ask

What characters should be escaped in JSON?

In JSON the only characters you must escape are \, ", and control codes.

Does JSON dump escape quotes?

Furthermore, all double quotes of the actual JSON formatting are escaped with a backslash.

How do you escape a new line character in JSON?

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.

What is the difference between JSON dump and JSON dumps?

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.


1 Answers

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.

like image 74
Martijn Pieters Avatar answered Sep 27 '22 21:09

Martijn Pieters