0

I have parsed some JSON which looks like this:

{"bag": "saco", "balloon": "bal\u00e3o", "apple": "ma\u00e7\u00e3"}

It's got us and the accents aren't displayed properly.

I have prefixed my Python file with

#!/usr/bin/env python
# -*- coding: utf-8 -*-

and I have a dictionary which I dump into the JSON file like this:

new_dict = {}
// doing stuff with the dictionary
with open('myFile.json', 'w+') as fp:
    json.dump(new_dict, fp)

Sorry I'm a newbie with Python. How can I make my script write the JSON file without the u and so that it displays the characters correctly?

Cesare
  • 9,139
  • 16
  • 78
  • 130
  • What do you mean by "display"? – ekhumoro Mar 24 '18 at 16:50
  • display `rádio` instead of `u'r\xe1dio'` in the JSON file @ekhumoro – Cesare Mar 24 '18 at 16:51
  • But display where, display how? – ekhumoro Mar 24 '18 at 16:53
  • 2
    _"How can I make my script write the JSON file without the u"_ `json.dump` doesn't write that `u`-prefix into the file. That wouldn't be valid json. – Aran-Fey Mar 24 '18 at 16:55
  • @ekhumoro I'm not sure where and how I want to display things, I'd hope I can get the "right" data into the JSON file – Cesare Mar 24 '18 at 16:58
  • thanks, I'll take a look at the links and at the output of `json.dump` – Cesare Mar 24 '18 at 16:58
  • I've updated the question so that it reflects a JSON file. I got the link (https://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence) but I truly don't know how to use this question (not even sure how to display accents). I feel lost. – Cesare Mar 24 '18 at 17:08
  • 2
    @Cesare. Why do you care about what is in the json file? The output now shown in your question is correct and normal json. If you convert it into a dictionary, it will contain unicode strings. The accents should be displayed correctly - but that depends on what you are using to view them. On my system, if I do `d = json.loads('{"balloon": "bal\u00e3o"}'); print d['balloon']`, the output is `balão`. However, on some platforms (esp. windows), the results may be different, depending on how and where you are trying to display the output. – ekhumoro Mar 24 '18 at 17:37

0 Answers0