add by zhj: 其實一般情況下,不會遇到變量c這種編碼的,往往是哪些出錯了,才會出現這種情況。所以遇到這種情況,要先
查看代碼,避免這種情況的出現
原文:https://mozillazg.com/2013/12/python-raw-unicode.html
見下面的代碼,我們知道,對於unicode字符串,是像b那樣的格式,即u'\u6211\u7231Python',而c中的包含的其實是utf-8編碼的,
a就是utf-8編碼的。那怎么去掉c前面的u呢?Python提供了方法unicode.encode('raw_unicode_escape')
In [91]: a = '我愛Python' In [92]: a Out[92]: '\xe6\x88\x91\xe7\x88\xb1Python' In [93]: b = u'我愛Python' In [94]: b Out[94]: In [95]: c = u'\xe6\x88\x91\xe7\x88\xb1Python' In [96]: print c # 亂碼 æç±Python In [97]: c Out[97]: u'\xe6\x88\x91\xe7\x88\xb1Python' In [98]: d = In [99]: d Out[99]: '\xe6\x88\x91\xe7\x88\xb1Python' In [100]: print d 我愛Python