Python中字符集转换问题问题

https://img1.mukewang.com/5b4eedf0000151cc12940416.jpg

如图,为什么最后 不能以中文显示呢?

慕侠2389804
浏览 480回答 3
3回答

MM们

print出来就行了,否则你看到的就是原始的十六进制编码。类似的还有list和tuple的print,你会发现一样是这样。In [2]: lst = ['中文1', '中文2', '测试3'] In [3]: print lst ['\xe4\xb8\xad\xe6\x96\x871', '\xe4\xb8\xad\xe6\x96\x872', '\xe6\xb5\x8b\xe8\xaf\x953'] In [4]: print ' '.join(lst) 中文1 中文2 测试3这个不是BUG,显示也是正确的,这是python2的机制。如果你用的是python3,那么情况大不一样,python3强制unicode编码,直接预览list/tuple之类的看到的就是解码过的字符。fengyu@fengyu-pc  ~  ipython3 Python 3.4.0 (default, Apr 11 2014, 13:05:11)  Type "copyright", "credits" or "license" for more information. IPython 1.2.1 -- An enhanced Interactive Python. ?         -> Introduction and overview of IPython's features. %quickref -> Quick reference. help      -> Python's own help system. object?   -> Details about 'object', use 'object??' for extra details. In [1]: lst = ['中文1', 'English', '测试3'] In [2]: lst Out[2]: ['中文1', 'English', '测试3'] In [3]:

偶然的你

Win8系统?str1='中文'print str1.decode('utf-8')

慕少森

print str1.decode(utf8).encode('gbk'),其中utf8不定,要看实际的编码方式(先decode到unicode, 然后再encode到gbk
打开App,查看更多内容
随时随地看视频慕课网APP