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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



'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.



here is my understanding:



I am using python3, so file fh is in bytes. I am encoding it into string and putting it in memory via StringIO.


fh



with csv.DictReader() trying to read it as dict into memory. It is failing here:


csv.DictReader()



enter image description 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? :/




1 Answer
1



The error is because there is some non-ASCII character in the file and it can't be encoded/decoded. 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):



a.decode('utf-8')


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


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



use 'rb' if your file is binary.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard