Reputation: 1759
I'm trying to convert a str string '\u4e2d\u56fd'
to unicode u'\u4e2d\u56fd'
. It's the same thing in python3, but how to do it in python2.7?
I have tried decode, encode, str, bytes, unicode and others, but they all didn't work.
a = '\u4e2d\u56fd'
b = u'\u4e2d\u56fd'
print a
print b
And the result of the code above is
\u4e2d\u56fd
中国
All I want to do is to convert a to b.
Thanks for any tip!
Upvotes: 0
Views: 327
Reputation: 197
You need to use raw_unicode_escape codec. For example:
a = '\u4e2d\u56fd'
a = a.decode('raw_unicode_escape')
print a
中国
Upvotes: 1
Reputation: 1924
you should write in the header of .py
file # -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
a = '\u4e2d\u56fd'
print a
or try that
import sys
reload(sys)
sys.setdefaultencoding('utf8')
Upvotes: 0