简体   繁体   English

如何摆脱python输出中的unicode字符?

[英]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. 我正在尝试加载json文件,然后尝试稍后解析。 however, in the output i keep getting the 'u' characters. 但是,在输出中,我不断得到'u'字符。 I tried to open the file with encoding='utf-8' which dint solve the problem. 我试图用encoding ='utf-8'打开​​文件,这可以解决问题。 i am using python 2.7. 我正在使用python 2.7。 is there a straight forward approach or workaround to ignore and get ride of the 'u' characters in the output. 是否有直接的方法或解决方法来忽略和获取输出中的'u'字符。

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 o / p

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

ps: 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 ps:我走过这篇文章抑制了python字符串中的u'prefix指示unicode',但这没有针对python 2.7的解决方案

Use json.dumps and decode it to convert it to string 使用json.dumps并将其解码以将其转换为字符串

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

u just indicates a Unicode string. u仅表示Unicode字符串。 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...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM