Python关于字符串编码,个人认识
2013-02-23 23:06
465 查看
朱金华 20130223
近期写了几个小脚本, 涉及到字符编码的时候总是失败, 无规律可循, 最后总结如下:
[1]文件里面引号括起来的字符串其编码与脚本文件编码一致.
若源文件为utf-8, 则str='朱金华'的编码为utf-8
对字符的处理一般都是先decode(当前编码) 使之变为unicode, 然后再encode(目标编码的)
[2]print打印, 这个显示中文字符需要与控制台编码一致. 譬如windows cmd默认是gbk的, 那么只有按照gbk或gb2312编码的才能显示
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编码 以便print
print'----------------------------------------------'
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'>
'''
近期写了几个小脚本, 涉及到字符编码的时候总是失败, 无规律可循, 最后总结如下:
[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=rawstr print 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编码 以便print
print'----------------------------------------------'
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的编码问题的个人小结
- Python2和Python3之间关于字符串编码处理的差别
- 关于python字符串编码问题的理解
- python基础(二)关于编码和字符串操作etc
- 关于python中的字符串编码理解
- 【python】 [基础] 数据类型,字符串和编码
- python字符串编码问题
- 关于python编码问题总结
- 关于 Python3 的编码
- Python获取字符串的编码
- python 解析unicode编码的字符串
- 飘逸的python - 编码杂症之在字符串前面加u
- python2与python3的字符串编码对比
- 关于Python中以字母r/R,或字母u/U 开头的字符串
- 小甲鱼python视频弟十二讲(关于字符串的方法及注释下)
- Python学习札记(五) Basic2 字符串和编码
- 在Python中关于中文编码问题的处理建议
- 关于Python中以字母r/R,或字母u/U 开头的字符串
- 第二章 Python字符串处理和编码不再发愁