UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)
2016-07-14 10:24
441 查看
Python2中的编码、解码问题
使用Python2处理中文或不同编码时,经常遇到如标题所示的问题,烦!通过查阅一些资料终于理解了其中的原理及解决办法。
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。
其中decode将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码。
而encode将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码。
所以在转码时一定要先看清字符串str是什么编码,然后decode成unicode,再encode成其他编码。如,在开头为#-*- coding: utf8 -*-的.py文件中,字符串的编码就是utf-8(代码中字符串的默认编码与代码文件本身的编码一致),以此类推。
如:str='中文',如果是在utf-8文件中,str就是utf-8编码,如果是在gbk的文件中,它就是gbk编码。此时如果要进行编码,都必须先用decode将str解码成unicode编码,然后再用encode转换成其他类型的编码。如果是直接进行encode,很有可能会发生UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)。
我看网上解决上述问题的方法主要有两种(以在# -*- coding: utf-8 -*- 文件中为例):
第一种:更改sys.defaultencoding为文件的编码方式
第二种:明确指出str的编码方式
参考资料:
http://www.cnblogs.com/bluescorpio/p/3594359.html
http://blog.csdn.net/crazyhacking/article/details/39375535
使用Python2处理中文或不同编码时,经常遇到如标题所示的问题,烦!通过查阅一些资料终于理解了其中的原理及解决办法。
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。
其中decode将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码。
而encode将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码。
所以在转码时一定要先看清字符串str是什么编码,然后decode成unicode,再encode成其他编码。如,在开头为#-*- coding: utf8 -*-的.py文件中,字符串的编码就是utf-8(代码中字符串的默认编码与代码文件本身的编码一致),以此类推。
如:str='中文',如果是在utf-8文件中,str就是utf-8编码,如果是在gbk的文件中,它就是gbk编码。此时如果要进行编码,都必须先用decode将str解码成unicode编码,然后再用encode转换成其他类型的编码。如果是直接进行encode,很有可能会发生UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)。
我看网上解决上述问题的方法主要有两种(以在# -*- coding: utf-8 -*- 文件中为例):
第一种:更改sys.defaultencoding为文件的编码方式
#! /usr/bin/env python # -*- coding: utf-8 -*- import sys reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入 sys.setdefaultencoding('utf-8') str = '中文' str.encode('gb18030')虽然第一种方法是万能钥匙,但是最好不要用,因为python3.x好像已经将这个方法删除了,具体原因请看http://bbs.csdn.net/topics/391900840
第二种:明确指出str的编码方式
#! /usr/bin/env python # -*- coding: utf-8 -*- s = '中文' s.decode('utf-8').encode('gb18030')
参考资料:
http://www.cnblogs.com/bluescorpio/p/3594359.html
http://blog.csdn.net/crazyhacking/article/details/39375535
相关文章推荐
- android EditText当设置中开启拼音检查工具报错问题
- 最近看了很多新闻只愿中华无恙
- 设置导航栏的背景颜色及属性
- CentOS下安装中文支持及字体
- Python 决策树
- 使用eclipse调试openfre3.9.3源码
- Mirantis OpenStack Fuel9.0离线安装(MOS9.0本地源)
- CodeChef CHEFSOC2 Chef and Big Soccer 水dp
- 小总结:字符串和数字的简单相互转换
- UVA - 10161 Ant on a Chessboard
- SVN使用
- MYSQL官方下載網址
- 多线程编程 - NSOperationQueue
- 百度地图 使用两条平行线表示路线
- flume安装和avro场景
- UVA - 113 Power of Cryptography
- Twitter使用时API报错Exception in thread "main" Connection refused: connect
- Android中区别Drawable Bitmap Canvas Paint
- 设计响应式网站的注意点
- XNA窗口设置问题