-3

When I load the JSON file and when I print it I get before each attribute a "u'"

how can I escape it ?

    try:
        with codecs.open('graphe.json', 'r', 'utf-8') as json_data:
            c = json.load(json_data)
            print c
    except IOError, e:
        print 'IOError : No file in input'

{u'ressourcepath': u'D:\Stage_ete_2016\DjangoProject\resources\', u'Nodes': [{u'title': [u'npq1', u'npq3', u'npq2'],....

the JSON

{"ressourcepath": "D:\Stage_ete_2016\DjangoProject\resources\", "Nodes": [{"title": ["npq1", "npq3", "npq2"],...

so the problem is that I use this dictionary to write a JavaScript code (template) and I must respect the JavaScript syntax (Vis js): the problem screenshot

warunapww
  • 966
  • 4
  • 18
  • 38
Djoubat
  • 3
  • 2
  • 3
    Why do you think you need to "delete" this character? What *practical* issue are you facing? – Daniel Roseman Aug 18 '16 at 11:58
  • You except most likely having the wrong indent and u'' is the Python way to tell you, you got an Unicode string – frlan Aug 18 '16 at 11:58
  • 1
    You're not trying to print json. You're printing python `dict` – Daniil Ryzhkov Aug 18 '16 at 12:00
  • There is no `u` character in the string. You look at the **representation** of the string where the leading `u` shows that the string contains unicode data. If you print the string itself you will see no `u`. – Matthias Aug 18 '16 at 12:26
  • I use this dict to build a JavaScript code (django template ) to pint a network (vis js) but so I must delete th "u'" so that it will be no error syntax. **here is the probleme : ** – Djoubat Aug 18 '16 at 12:47

1 Answers1

2

The u prefix means that those strings are unicode rather than 8-bit strings. The best way to not show the u prefix is to switch to Python 3, where strings are unicode by default. If that's not an option, the str constructor will convert from unicode to 8-bit, so simply loop recursively over the result and convert unicode to str. However, it is probably best just to leave the strings as unicode.

Shaig Khaligli
  • 4,955
  • 5
  • 22
  • 32