[英]'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.