简体   繁体   English

使用python显示Unicode字符

[英]Displaying unicode characters using python

I have a string that prints out like this 我有一个像这样打印出来的字符串

print a
\u4f53\u91cd\u8a08

I am using eclipse and the console can print unicode characters, I have tested it like this. 我正在使用eclipse,并且控制台可以打印unicode字符,我已经对此进行了测试。

print u'\u4f53\u91cd\u8a08'
体重計

It prints out correctly, how can I make the string in the variable a to be printed like above. 它可以正确打印,我如何像上面那样在变量a中创建字符串。

Thanks very much for advance. 非常感谢您的提前。

Perhaps... 也许...

print unicode(a)

would do the trick? 会成功吗?

If the string itself actually has escaped escapes in it (ie if you were to write it, it'd be something more like u'\\\体\\\重\\\計' ), then use: 如果字符串本身实际上已经在其中转义了(即,如果您要编写它,那将更像是u'\\\体\\\重\\\計' ),然后使用:

print a.decode('unicode-escape')

Decode using the unicode-escape codec. 使用unicode-escape编解码器进行解码。

>>> print '\\u4f53\\u91cd\\u8a08'.decode('unicode-escape')
体重計

Or if what you have is actually a JSON fragment, decode as JSON. 或者,如果您实际上拥有的是JSON片段,则将其解码为JSON。

>>> print json.loads('"\\u4f53\\u91cd\\u8a08"')
体重計

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

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