python 中文字符串的处理实现代码
Jun 06, 2016 am 11:26 AM
>>> teststr = '我的eclipse不能正确的解码gbk码!'
>>> teststr
'\xe6\x88\x91\xe7\x9a\x84eclipse\xe4\xb8\x8d\xe8\x83\xbd\xe6\xad\xa3\xe7\xa1\xae\xe7\x9a\x84\xe8\xa7\xa3\xe7\xa0\x81gbk\xe7\xa0\x81\xef\xbc\x81'
>>> tests2 = u'我的eclipse不能正确的解码gbk码!'
>>> test3 = tests2.encode('gb2312')
>>> test3
'\xce\xd2\xb5\xc4eclipse\xb2\xbb\xc4\xdc\xd5\xfd\xc8\xb7\xb5\xc4\xbd\xe2\xc2\xebgbk\xc2\xeb\xa3\xa1'
>>> test3
'\xce\xd2\xb5\xc4eclipse\xb2\xbb\xc4\xdc\xd5\xfd\xc8\xb7\xb5\xc4\xbd\xe2\xc2\xebgbk\xc2\xeb\xa3\xa1'
>>> teststr
'\xe6\x88\x91\xe7\x9a\x84eclipse\xe4\xb8\x8d\xe8\x83\xbd\xe6\xad\xa3\xe7\xa1\xae\xe7\x9a\x84\xe8\xa7\xa3\xe7\xa0\x81gbk\xe7\xa0\x81\xef\xbc\x81'
>>> test3.decode('gb2312').encode('utf-8')
'\xe6\x88\x91\xe7\x9a\x84eclipse\xe4\xb8\x8d\xe8\x83\xbd\xe6\xad\xa3\xe7\xa1\xae\xe7\x9a\x84\xe8\xa7\xa3\xe7\xa0\x81gbk\xe7\xa0\x81\xef\xbc\x81'
>>> test3.decode('gb2312').encode('utf-8') == teststr
True
如上所见,test3变量(gb2312编码)经过解码(变成unicode字符串)后再使用utf-8编码,就成了与teststr值相同的串了.
通过上面的例子我们也发现,unicode字符串是gb2312字符串(windows就使用这种格式)与utf-8字符串(python本身使用)之间的一座桥梁.

熱門文章

熱門文章

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2

只要250美元,Hugging Face技術主管手把手教你微調Llama 3
