lucastimmons
lucastimmons

Reputation: 151

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

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?

Upvotes: 5

Views: 1959

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123400

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.

Upvotes: 9

Related Questions