Python关于字符串编码,个人认识
来源:互联网 发布:淘宝网设计 编辑:程序博客网 时间:2024/06/14 09:32
朱金华 20130223
近期写了几个小脚本, 涉及到字符编码的时候总是失败, 无规律可循, 最后总结如下:
[1]文件里面引号括起来的字符串其编码与脚本文件编码一致.
若源文件为utf-8, 则str='朱金华'的编码为utf-8
对字符的处理一般都是先decode(当前编码) 使之变为unicode, 然后再encode(目标编码的)
[2]print打印, 这个显示中文字符需要与控制台编码一致. 譬如windows cmd默认是gbk的, 那么只有按照gbk或gb2312编码的才能显示
rawstr='zjh123朱金华'print rawstr,repr(rawstr),type(rawstr)str=rawstr.decode('utf-8').encode('utf-8')print str,repr(str),type(str)str=rawstr.decode('utf-8')print str,repr(str),type(str)str=rawstr.decode('utf-8').encode('gbk')print str,repr(str),type(str)print'----------------------------------------------'rawstr=u'zjh123朱金华'print rawstr,repr(rawstr),type(rawstr)str=rawstr.encode('utf-8')print str,repr(str),type(str)str=rawstrprint str,repr(str),type(str)str=rawstr.encode('gbk')print str,repr(str),type(str)输出: cmd位GBK编码
zjh123鏈遍噾鍗?'zjh123\xe6\x9c\xb1\xe9\x87\x91\xe5\x8d\x8e' <type 'str'>
zjh123鏈遍噾鍗?'zjh123\xe6\x9c\xb1\xe9\x87\x91\xe5\x8d\x8e' <type 'str'>
zjh123朱金华 u'zjh123\u6731\u91d1\u534e' <type 'unicode'>
zjh123朱金华 'zjh123\xd6\xec\xbd\xf0\xbb\xaa' <type 'str'>
----------------------------------------------
zjh123朱金华 u'zjh123\u6731\u91d1\u534e' <type 'unicode'>
zjh123鏈遍噾鍗?'zjh123\xe6\x9c\xb1\xe9\x87\x91\xe5\x8d\x8e' <type 'str'>
zjh123朱金华 'zjh123\xd6\xec\xbd\xf0\xbb\xaa' <type 'str'>
总结:
rawstr='zjh123朱金华'print rawstr,repr(rawstr),type(rawstr)str=rawstr.decode('utf-8').encode('utf-8')print str,repr(str),type(str) #这两个的编码是一致的, 也说明原先就是utf-8编码的 'zjh123\xe6\x9c\xb1\xe9\x87\x91\xe5\x8d\x8e' print显示乱码str=rawstr.decode('utf-8')print str,repr(str),type(str) # #按当前编码utf-8解码后得到 unicode u'zjh123\u6731\u91d1\u534e' <type 'unicode'>str=rawstr.decode('utf-8').encode('gbk')print str,repr(str),type(str) #将unicode数据再按gbk编码 以便printprint'----------------------------------------------'rawstr=u'zjh123朱金华'print rawstr,repr(rawstr),type(rawstr) #直接就是unicode编码str=rawstr.encode('utf-8')print str,repr(str),type(str) #按照utf-8编码, 不用先解码了, 解码就出错了str=rawstr.encode('gbk')print str,repr(str),type(str) #可以打印
将源文件编码为ANSI格式后, 实际就是windows所谓内码, 目前测试环境是GBK了
rawstr='zjh123朱金华'print rawstr,repr(rawstr),type(rawstr) #原编码后'zjh123\xd6\xec\xbd\xf0\xbb\xaa' 与上面的gbk编码是一样的str=rawstr.decode('gbk').encode('utf-8') #编码为utf-8 'zjh123\xe6\x9c\xb1\xe9\x87\x91\xe5\x8d\x8e'print str,repr(str),type(str) str=rawstr.decode('gb2312').encode('gbk') #'zjh123\xd6\xec\xbd\xf0\xbb\xaa' gbk是gb2312的扩展, 当然编码还是一样的了print str,repr(str),type(str)'''输出为:zjh123朱金华 'zjh123\xd6\xec\xbd\xf0\xbb\xaa' <type 'str'>zjh123鏈遍噾鍗?'zjh123\xe6\x9c\xb1\xe9\x87\x91\xe5\x8d\x8e' <type 'str'>zjh123朱金华 'zjh123\xd6\xec\xbd\xf0\xbb\xaa' <type 'str'>'''
- Python关于字符串编码,个人认识
- 关于 python 字符编码的一些认识
- 关于python中的字符串编码
- 关于python字符串编码问题的理解
- 关于python的编码问题的个人小结
- python基础(二)关于编码和字符串操作etc
- 关于汇率,个人的一些认识
- Python字符串编码
- python字符串编码常见问题
- python字符串编码问题
- python处理字符串编码
- Python 中的字符串编码
- Python 中的字符串编码
- python字符串编码常见问题
- python字符串编码
- Python 字符串编码
- python字符串编码问题
- python字符串编码判断
- hdu 3068 poj 3974 回文串
- 7个超级实用的PHP代码片段分享
- 笑傲江湖高清下载
- DirectX11教程5-贴图
- 教你如何通过Mysql弱口令得到系统权限
- Python关于字符串编码,个人认识
- 外行人摸github
- 《在一起下载》下载
- 《一代宗师》下载
- C#获取网页内容的三种方式(转)
- Android中String与StringBuilder有什么区别?
- 我的php学习笔记(十九)php的面向对象开发(六)
- 1680 Catalan数
- 《越来越好之村晚》下载