-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

5
  • 3
    Why do you think you need to "delete" this character? What practical issue are you facing? Commented Aug 18, 2016 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 Commented Aug 18, 2016 at 11:58
  • 1
    You're not trying to print json. You're printing python dict Commented Aug 18, 2016 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. Commented Aug 18, 2016 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 : ** Commented Aug 18, 2016 at 12:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I must parse all the dict and then apply the str constructor to each key/value that is the only solution ?
exactly, you are right!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.