如何从unicode字节表示检索我的unicode [英] How to retrieve my unicode from the unicode byte representation

查看:194
本文介绍了如何从unicode字节表示检索我的unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个unicode字符串κανω但是由于一些其他软件的一些预处理,我不能改变它成为一个字符串'\\ \\ u03ba\\\α\\\ν\\\ω'而不是 u'\\\κ\\\α\\\ν\\\ω'

I have an unicode string κανω but due to some preprocessing from some other software that I can't change it becomes a literal string '\u03ba\u03b1\u03bd\u03c9' instead of u'\u03ba\u03b1\u03bd\u03c9'.

如何更改'\\\κ\\\α\\\ν\\\ω' u'\\\κ\\\α\\\ν\\\ω'

How could I change '\u03ba\u03b1\u03bd\u03c9' back to u'\u03ba\u03b1\u03bd\u03c9'?

已尝试:

>>> x = '\u03ba\u03b1\u03bd\u03c9'
>>> print x
\u03ba\u03b1\u03bd\u03c9
>>> print x.decode('utf8')
\u03ba\u03b1\u03bd\u03c9
>>> print x.encode('utf8')
\u03ba\u03b1\u03bd\u03c9
>>> print unicode(x)
\u03ba\u03b1\u03bd\u03c9

我不能去每个字符串输出并添加 u'...',即我需要避免这样做:

I cannot possibly go to each string output and add the u'...', i.e. I need to avoid doing this:

>>> x = u'\u03ba\u03b1\u03bd\u03c9'
>>> print x
κανω


推荐答案

href =https://docs.python.org/2/library/codecs.html#python-specific-encodings =nofollow> 'unicode_escape' (在Python源代码中生成适合作为Unicode字面量的字符串)作为其编码:

You need 'unicode_escape' (Produce a string that is suitable as Unicode literal in Python source code) as its encoding :

>>> s='\u03ba\u03b1\u03bd\u03c9'
>>> print unicode(s,'unicode_escape')
κανω

这篇关于如何从unicode字节表示检索我的unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆