手机
当前位置:查字典教程网 >脚本专栏 >python >python 中文字符串的处理实现代码
python 中文字符串的处理实现代码
摘要:>>>teststr='我的eclipse不能正确的解码gbk码!'>>>teststr'xe6x88x91xe7x9ax84eclipse...

>>> teststr = '我的eclipse不能正确的解码gbk码!'

>>> teststr

'xe6x88x91xe7x9ax84eclipsexe4xb8x8dxe8x83xbdxe6xadxa3xe7xa1xaexe7x9ax84xe8xa7xa3xe7xa0x81gbkxe7xa0x81xefxbcx81'

>>> tests2 = u'我的eclipse不能正确的解码gbk码!'

>>> test3 = tests2.encode('gb2312')

>>> test3

'xcexd2xb5xc4eclipsexb2xbbxc4xdcxd5xfdxc8xb7xb5xc4xbdxe2xc2xebgbkxc2xebxa3xa1'

>>> test3

'xcexd2xb5xc4eclipsexb2xbbxc4xdcxd5xfdxc8xb7xb5xc4xbdxe2xc2xebgbkxc2xebxa3xa1'

>>> teststr

'xe6x88x91xe7x9ax84eclipsexe4xb8x8dxe8x83xbdxe6xadxa3xe7xa1xaexe7x9ax84xe8xa7xa3xe7xa0x81gbkxe7xa0x81xefxbcx81'

>>> test3.decode('gb2312').encode('utf-8')

'xe6x88x91xe7x9ax84eclipsexe4xb8x8dxe8x83xbdxe6xadxa3xe7xa1xaexe7x9ax84xe8xa7xa3xe7xa0x81gbkxe7xa0x81xefxbcx81'

>>> test3.decode('gb2312').encode('utf-8') == teststr

True

如上所见,test3变量(gb2312编码)经过解码(变成unicode字符串)后再使用utf-8编码,就成了与teststr值相同的串了.

通过上面的例子我们也发现,unicode字符串是gb2312字符串(windows就使用这种格式)与utf-8字符串(python本身使用)之间的一座桥梁.

【python 中文字符串的处理实现代码】相关文章:

python 输出一个两行字符的变量

Python中文编码那些事

python 数据加密代码

Python 初始化多维数组代码

python 文件与目录操作

python 查找文件夹下所有文件 实现代码

Python MD5文件生成码

Python 可爱的大小写

python计算最小优先级队列代码分享

python 提取文件的小程序

精品推荐
分类导航