首先:中文->unicode字符串
import chardet
import codecs
>>> a = "我是一个中国人"
>>> a
‘\xce\xd2\xca\xc7\xd2\xbb\xb8\xf6\xd6\xd0\xb9\xfa\xc8\xcb‘
>>> chardet.detect(a)
{‘confidence‘: 0.99, ‘encoding‘: ‘GB2312‘}
>>> b = a.decode(‘GB2312‘)
>>> b
u‘\u6211\u662f\u4e00\u4e2a\u4e2d\u56fd\u4eba‘ -->但是我们往往不想要外面的u
>>> c =repr(b)
>>> c
"u‘\\u6211\\u662f\\u4e00\\u4e2a\\u4e2d\\u56fd\\u4eba‘"
>>> d = c[2:-1]
>>> d
‘\\u6211\\u662f\\u4e00\\u4e2a\\u4e2d\\u56fd\\u4eba‘
>>> print d
\u6211\u662f\u4e00\u4e2a\u4e2d\u56fd\u4eba
然后是 unicode ->中文:
>>> a = ‘\u9a8c‘
>>> a
‘\\u9a8c‘
>>> b = ‘u‘+‘\‘‘+a+‘\‘‘
>>> b
"u‘\\u9a8c‘"
>>> c = eval(b)
>>> c
u‘\u9a8c‘
>>> print c
验
python中文和unicode字符串之间的互相转换,布布扣,bubuko.com
原文:http://wangyincol.blog.51cto.com/3415656/1535794