手机
当前位置:查字典教程网 >脚本专栏 >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 open读写文件实现脚本

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

python 文件与目录操作

Python字符遍历的艺术

python 测试实现方法

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

Python中文编码那些事

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

Python 时间处理datetime实例

精品推荐
分类导航