您的位置:首页 > 其它

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

2018-03-06 13:14 411 查看
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为文件的编码方式
[python] view plain copy#! /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的编码方式
[python] view plain copy#! /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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐