unicode 和 str(Python字符编码解码)
2017-04-23 00:58
453 查看
python读写字符串数据,最痛苦的事情之一就是编码的问题,这里收集并整理一下
ed在编码解码问题上一般会出现的几种问题
1.为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“/xe4/xb8/xad/xe6/x96/x87”的形式?为什么会报错“UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1: ordinal not in range(128)”?
2.为什么读进去是”风卷残云”,输出来却是”椋庡嵎娈嬩簯”?
最后c的乱码,问题在于终端的字符集,跟python语言本身无关.为了保证输出不会在 linux 终端上显示乱码,需要设置好 linux 的环境变量:export LANG=en_US.UTF-8,同时将字符串编码成utf-8,后面会介绍如何检测字符串的编码.
在python中,直接将某种编码的str进行encode成另一种编码str
Tips: 用chardet进行文件和字符串编码检测
\u字符串转对应unicode字符串
[参考文献]
1. http://www.cnblogs.com/skynet/archive/2011/05/03/2035105.html
2. http://blog.csdn.net/ktb2007/article/details/3876436
3. http://in355hz.iteye.com/blog/1860787
4. http://wklken.me/posts/2013/08/31/python-extra-coding-intro.html
ed在编码解码问题上一般会出现的几种问题
1.为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“/xe4/xb8/xad/xe6/x96/x87”的形式?为什么会报错“UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1: ordinal not in range(128)”?
2.为什么读进去是”风卷残云”,输出来却是”椋庡嵎娈嬩簯”?
基本概念
字符串在Python内部的表示是unicode编码.字符串的表示通常都有一个过程.从源编码通过decode()解码并编码成unicode, 然后通过encode()解utf-8编码并编码乘目标编码.在不设置decode(), encode()的参数时, 采用的是系统默认的编解码方法.查看和修改系统默认编码过程:>>> import sys >>> sys.getdefaultencoding() 'ascii' >>> reload(sys) ##### important >>> sys.setdefaultencoding('utf-8') >>> a = '中国' >>> a '\xe4\xb8\xad\xe5\x9b\xbd' >>> b = a.decode() UnicodeDecodeError: 'ascii' codec cant decode byte 0xe4 in position 0: ordinal not in range(128) >>> b = a.decode('utf-8') >>> b u'\u4e2d\u56fd' >>> c = b.encode('gb2312') >>> c '\xd6\xd0\xb9\xfa' >>> print c �й
最后c的乱码,问题在于终端的字符集,跟python语言本身无关.为了保证输出不会在 linux 终端上显示乱码,需要设置好 linux 的环境变量:export LANG=en_US.UTF-8,同时将字符串编码成utf-8,后面会介绍如何检测字符串的编码.
str和unicode
decode()等于strToUnicode, encode()等于unicodeToStr()在python中,直接将某种编码的str进行encode成另一种编码str
#str_A为utf-8 str_A.encode('gbk') # 执行的操作是 str_A.decode('sys_codec').encode('gbk') 这里sys_codec即为sys.getdefaultencoding() 的编码
Tips: 用chardet进行文件和字符串编码检测
>>> import chardet >>> f = open('test.txt','r') >>> result = chardet.detect(f.read()) # type(result) = str >>> result {'confidence': 0.99, 'encoding': 'utf-8'}
\u字符串转对应unicode字符串
>>> u'中' u'\u4e2d' >>> s = '\u4e2d' >>> print s.decode('unicode_escape') 中 >>> a = '\\u4fee\\u6539\\u8282\\u70b9\\u72b6\\u6001\\u6210\\u529f' >>> a.decode('unicode_escape') u'\u4fee\u6539\u8282\u70b9\u72b6\u6001\u6210\u529f'
[参考文献]
1. http://www.cnblogs.com/skynet/archive/2011/05/03/2035105.html
2. http://blog.csdn.net/ktb2007/article/details/3876436
3. http://in355hz.iteye.com/blog/1860787
4. http://wklken.me/posts/2013/08/31/python-extra-coding-intro.html
相关文章推荐
- python字符编码与解码 unicode,str
- python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode
- python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode
- 【转】python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode
- python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode
- python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode 分类: python
- python 字符编码与解码——unicode、str和中文:UnicodeDecodeError: 'ascii' codec can't decode
- python的str,unicode对象的encode和decode方法, Python中字符编码的总结和对比bytes和str
- Python中字符编码的总结和对比:Python 2.x的str和unicode vs Python 3.x的bytes和str
- python的str,unicode对象的encode和decode方法, Python中字符编码的总结和对比bytes和str
- python下字符编码和解码问题:UnicodeEncodeError: 'gbk' codec can't encode character u'\xa0'
- python2.7 中unicode、str的编解码关系
- python2.7 中unicode、str的编解码关系
- Python中的str与unicode处理方法
- 关于python的str和unicode以及编码的问题
- Python 2.x中常见字符编码和解码方面的错误及其解决办法 总结
- Python中的str与unicode处理方法
- python unicode 及解码编码方式简介
- Python中的str与unicode处理方法
- Python 字符编码学习-ascii编码,Unicode和UTF-8之间的关系