简体   繁体   English

'utf-8'编解码器无法解码位置170的字节0x8a:无效的起始字节

[英]'utf-8' codec can't decode byte 0x8a in position 170: invalid start byte

I am trying to do this: 我正在尝试这样做:

fh = request.FILES['csv']
fh = io.StringIO(fh.read().decode())
reader = csv.DictReader(fh, delimiter=";")

This is failing always with the error in title and I spent almost 8 hours on this. 这总是会因标题错误而失败,我为此花费了将近8个小时。

here is my understanding: 这是我的理解:

I am using python3, so file fh is in bytes. 我正在使用python3,因此文件fh以字节为单位。 I am encoding it into string and putting it in memory via StringIO. 我将其编码为字符串,并通过StringIO将其放入内存。

with csv.DictReader() trying to read it as dict into memory. csv.DictReader()尝试将其作为字典读入内存。 It is failing here: 它在这里失败:

在此处输入图片说明

also tried with io.StringIO(fh.read().decode('utf-8')) , but same error. 也尝试了io.StringIO(fh.read().decode('utf-8')) ,但同样的错误。

what am I missing? 我想念什么? :/ :/

The error is because there is some non-ASCII character in the file and it can't be encoded/decoded. 该错误是因为文件中包含一些非ASCII字符,并且无法对其进行编码/解码。 One simple way to avoid this error is to encode/decode such strings with encode()/decode() function as follows (if a is the string with non-ASCII character): 避免此错误的一种简单方法是使用如下的encode()/ decode()函数对此类字符串进行编码/解码(如果a是具有非ASCII字符的字符串):

a.decode('utf-8')

Also, you could try opening the file as: 另外,您可以尝试按以下方式打开文件:

with open('filename', 'r', encoding = 'utf-8') as f: your code using f as file pointer

use 'rb' if your file is binary. 如果您的文件是二进制文件,请使用“ rb”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'utf-8'编解码器无法解码位置14的字节0x97:无效的起始字节 - 'utf-8' codec can't decode byte 0x97 in position 14: invalid start byte UnicodeDecodeError:'utf-8'编解码器无法解码位置0的字节0x99:无效的起始字节 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x99 in position 0: invalid start byte UnicodeDecodeError'utf-8'编解码器无法解码位置2893中的字节0x92:无效的起始字节 - UnicodeDecodeError 'utf-8' codec can't decode byte 0x92 in position 2893: invalid start byte UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 1551 中的字节 0x87:起始字节无效 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x87 in position 1551: invalid start byte “utf-8”编解码器无法解码位置 0 中的字节 0x89:起始字节无效 - 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte “utf-8”编解码器无法解码 position 107 中的字节 0x92:无效的起始字节 - 'utf-8' codec can't decode byte 0x92 in position 107: invalid start byte Python:UnicodeDecodeError:'utf-8'编解码器无法解码 position 中的字节 0x80 0:无效起始字节 - Python: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte UnicodeDecodeError:“utf-8”编解码器无法解码 position 35 中的字节 0x96:无效的起始字节 - UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 35: invalid start byte “utf-8”编解码器无法解码 position 16 中的字节 0x82:无效的起始字节 - 'utf-8' codec can't decode byte 0x82 in position 16: invalid start byte “utf-8”编解码器无法解码 position 中的字节 0x8b 0:无效的起始字节 django - 'utf-8' codec can't decode byte 0x8b in position 0: invalid start byte django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM