手机
当前位置:查字典教程网 >脚本专栏 >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 过滤字符串的技巧,map与itertools.imap

py中的目录与文件判别代码

Python字符串的encode与decode研究心得乱码问题解决方法

python 测试实现方法

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

Python open读写文件实现脚本

Python 文件重命名工具代码

Python中文编码那些事

pydev使用wxpython找不到路径的解决方法

python 生成目录树及显示文件大小的代码

精品推荐
分类导航