cool77
cool77

Reputation: 1136

how to get rid of unicode characters in python output?

i am trying to load a json file and then trying to parse it later. however, in the output i keep getting the 'u' characters. I tried to open the file with encoding='utf-8' which dint solve the problem. i am using python 2.7. is there a straight forward approach or workaround to ignore and get ride of the 'u' characters in the output.

import json
import io


with io.open('/tmp/install-report.json', encoding='utf-8') as json_data:
    d = json.load(json_data)
    print d

o/p

{u'install': {u'Status': u'In Progress...', u'StartedAt': 1471772544,}}

p.s: i went trough this post Suppress the u'prefix indicating unicode' in python strings but that doesn't have a solution for python 2.7

Upvotes: 0

Views: 1702

Answers (2)

Mark Tolonen
Mark Tolonen

Reputation: 177600

u just indicates a Unicode string. If you print the string, it won't be displayed:

d = {u'install': {u'Status': u'In Progress...', u'StartedAt': 1471772544}}

print 'Status:',d[u'install'][u'Status']

Output:

Status: In Progress...

Upvotes: 0

Naruto
Naruto

Reputation: 4329

Use json.dumps and decode it to convert it to string

data = json.dumps(d, ensure_ascii=False).decode('utf8')
print data

Upvotes: 2

Related Questions