About UnicodeDecodeError: 'utf8' codec can't decode byte 0xa1 in position 157: invalid start byte
2014-10-17 13:48
639 查看
该问题是由于在decode时出现了一个非法字符,可设置在解码时忽略这个错误,
解决方案:
使用
import codecs
提供的方法解决
csvFile = codecs.open(csvfilename, u'rb',encoding= u'utf-8',errors='ignore')
这是ptyhon help 文档的内容
codecs.open(filename,
mode[, encoding[,
errors[, buffering]]])
Open an encoded file using the given mode and return a wrapped version providing transparent encoding/decoding. The default file mode is
'r' meaning to open the file in read mode.
Note
The wrapped version will only accept the object format defined by the codecs, i.e. Unicode objects for most built-in codecs. Output is also codec-dependent and will usually be Unicode as well.
Note
Files are always opened in binary mode, even if no binary mode was specified. This is done to avoid data loss due to encodings using 8-bit values. This means that no automatic conversion of
'\n' is done on reading and writing.
encoding specifies the encoding which is to be used for the file.
errors may be given to define the error handling. It defaults to
'strict' which causes a
ValueError to be raised in case an encoding error occurs.
buffering has the same meaning as for the built-in
open() function. It defaults to line buffered.
解决方案:
使用
import codecs
提供的方法解决
csvFile = codecs.open(csvfilename, u'rb',encoding= u'utf-8',errors='ignore')
这是ptyhon help 文档的内容
codecs.open(filename,
mode[, encoding[,
errors[, buffering]]])
Open an encoded file using the given mode and return a wrapped version providing transparent encoding/decoding. The default file mode is
'r' meaning to open the file in read mode.
Note
The wrapped version will only accept the object format defined by the codecs, i.e. Unicode objects for most built-in codecs. Output is also codec-dependent and will usually be Unicode as well.
Note
Files are always opened in binary mode, even if no binary mode was specified. This is done to avoid data loss due to encodings using 8-bit values. This means that no automatic conversion of
'\n' is done on reading and writing.
encoding specifies the encoding which is to be used for the file.
errors may be given to define the error handling. It defaults to
'strict' which causes a
ValueError to be raised in case an encoding error occurs.
buffering has the same meaning as for the built-in
open() function. It defaults to line buffered.
相关文章推荐
- Pickle:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
- ride.py中文用户名:UnicodeDecodeError:'utf8'codec can't decode byte 0xb2 in position 9: invalid start byte
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 884: invalid start byte
- python UnicodeDecodeError: 'utf8' codec can't decode byte 0xd6 in position 15: invalid continuation
- python提示UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xa1 in position 6: invalid start byte
- jupyter另存为py文件时:nbconvert failed: 'utf8' codec can't decode byte 0xc0 in position 96: invalid start
- 【转】UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 1: invalid continuation 汉字编码
- 'utf8' codec can't decode byte 0x8b in position 14: invalid start byte
- 手把手教你:python读取.htm文件报错:UnicodeDecodeError: 'utf8' codec can't decode byte 0xb3 in position 0的解决方法
- Python爬虫报UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 225: invalid start byt
- SSD Tensorflow:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start
- 报错信息:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 6382: invalid start byte
- 【转】UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 1: invalid continuation 汉字编码
- error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
- python 编码问题 UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xb4 in position 0: invalid start byt
- UnicodeDecodeError: 'utf8' codec can't decode byte 0xd5 in position 8: invalid c ontinuation byte
- UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 : invalid start byte
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xc1 in position 7: ordinal not in range(128)
- python中尝试df.to_excel的时候'utf8' codec can't decode byte 0xb8 in position 0: invalid start byte
- Python2.7编码问题:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position...解决方法