手机
当前位置:查字典教程网 >脚本专栏 >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 MD5文件生成码

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

Python中文编码那些事

Python 可爱的大小写

PHP webshell检查工具 python实现代码

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

python getopt 参数处理小示例

Python open读写文件实现脚本

精品推荐
分类导航