How to retrieve my unicode from the unicode byte r

2019-08-07 01:38发布

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'.

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

I've tried:

>>> 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

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
κανω

1条回答
一夜七次
2楼-- · 2019-08-07 02:09

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')
κανω
查看更多
登录 后发表回答