Using encode UTF-8 on File.read()

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am trying to upload a csv file to a postgresql database and it is stuck with a error you see at the end of my question. The reason is there are unicode characters">characters in the file and it is encoded in windows-1252.

This is the line where I decode the file with UTF-8. However I would like to basically accept every encoding and decode it as UTF-8 or set the encoding to UTF-8 when reading the file and then decode with the line down below. I am not using open because I had problems with it, instead I am using InMemoryUploadedFile.read() (https://docs.djangoproject.com/en/2.2/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile.read)

csv_file.seek(0)
file = csv_file.read().decode('utf-8').splitlines()
reader = csv.reader(file)

This is the error and it is because of this Character: d�mpe

'utf-8' codec can't decode byte 0xb3 in position 13969: invalid start byte

Any help would be appreciated.

profilepic.png
manpreet 2 years ago

You can use the errors parameter to .decode() to ignore encoding errors or replace them with a replacement character.

csv_file.seek(0)
file = csv_file.read().decode('utf-8', errors='ignore').splitlines()
reader = csv.reader(file)

It would be better, of course, to fix the original file so that it is actual, correct UTF-8.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.