python转码问题: in position 30664-30665: illegal multibyte sequence
2016-12-08 19:38
411 查看
在Python中,可以对String调用decode和encode方法来实现转码。
比如,若要将某个String对象s从gbk内码转换为UTF-8,可以如下操作
s.decode('gbk').encode('utf-8′)
可是,在实际开发中,我发现,这种办法经常会出现异常:
UnicodeDecodeError: ‘gbk' codec can't decode bytes in position 30664-30665: illegal multibyte sequence
这 是因为遇到了非法字符——尤其是在某些用C/C++编写的程序中,全角空格往往有多种不同的实现方式,比如\xa3\xa0,或者\xa4\x57,这些 字符,看起来都是全角空格,但它们并不是“合法”的全角空格(真正的全角空格是\xa1\xa1),因此在转码的过程中出现了异常。
这样的问题很让人头疼,因为只要字符串中出现了一个非法字符,整个字符串——有时候,就是整篇文章——就都无法转码。
解决办法:
s.decode('gbk', ‘ignore').encode('utf-8′)
因为decode的函数原型是decode([encoding], [errors='strict']),可以用第二个参数控制错误处理的策略,默认的参数就是strict,代表遇到非法字符时抛出异常;
如果设置为ignore,则会忽略非法字符;
如果设置为replace,则会用?取代非法字符;
如果设置为xmlcharrefreplace,则使用XML的字符引用。
pytho
4000
n文档
decode( [encoding[, errors]])
Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding. errors may be given to set a different error handling scheme. The default is 'strict', meaning that encoding errors raise UnicodeError. Other possible
values are 'ignore', 'replace' and any other name registered via codecs.register_error, see section 4.8.1.
比如,若要将某个String对象s从gbk内码转换为UTF-8,可以如下操作
s.decode('gbk').encode('utf-8′)
可是,在实际开发中,我发现,这种办法经常会出现异常:
UnicodeDecodeError: ‘gbk' codec can't decode bytes in position 30664-30665: illegal multibyte sequence
这 是因为遇到了非法字符——尤其是在某些用C/C++编写的程序中,全角空格往往有多种不同的实现方式,比如\xa3\xa0,或者\xa4\x57,这些 字符,看起来都是全角空格,但它们并不是“合法”的全角空格(真正的全角空格是\xa1\xa1),因此在转码的过程中出现了异常。
这样的问题很让人头疼,因为只要字符串中出现了一个非法字符,整个字符串——有时候,就是整篇文章——就都无法转码。
解决办法:
s.decode('gbk', ‘ignore').encode('utf-8′)
因为decode的函数原型是decode([encoding], [errors='strict']),可以用第二个参数控制错误处理的策略,默认的参数就是strict,代表遇到非法字符时抛出异常;
如果设置为ignore,则会忽略非法字符;
如果设置为replace,则会用?取代非法字符;
如果设置为xmlcharrefreplace,则使用XML的字符引用。
pytho
4000
n文档
decode( [encoding[, errors]])
Decodes the string using the codec registered for encoding. encoding defaults to the default string encoding. errors may be given to set a different error handling scheme. The default is 'strict', meaning that encoding errors raise UnicodeError. Other possible
values are 'ignore', 'replace' and any other name registered via codecs.register_error, see section 4.8.1.
相关文章推荐
- python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
- python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
- 关于编码问题,报错:'gbk' codec can't encode character '\u3164' in position 0: illegal multibyte sequence
- python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
- Python: 'gbk' codec can't encode character '\u30fb' in position 0: illegal multibyte sequence
- Python错误 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence
- 【Python】Python读取文件报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0x99 in position 20: illegal multibyte sequence
- 【python问题解决】UnicodeDecodeError :'gb2312' codec can't decode bytes:illegal multibyte sequence
- Python中转码错误遇到 illegal multibyte sequence
- python转码时出现'illegal multibyte sequen'错误
- python | 读文件编码问题 | UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 34: illegal mu
- 【UnicodeDecodeError: '' codec can't decode bytes in position : illegal multibyte sequence】
- Python读取CSV文件:UnicodeDecodeError: 'gbk' codec can't decode byte 0xba ....illegal multibyte sequence
- UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 46:illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 1425: illegal multibyte sequence
- Python2.7:UnicodeDecodeError :'gb2312' codec can't decode bytes:illegal multibyte sequence
- 读写文件中遇到'gbk' codec can't decode byte 0xa6 in position 36: illegal multibyte sequence
- 'gbk' codec can't encode character '\xa0' in position 1534: illegal multibyte sequence
- UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
- Python中读取txt文本出现“ 'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence”的解决办法