[Fixed]-Processing a Django UploadedFile as UTF-8 with universal newlines

11๐Ÿ‘

โœ…

As mentioned above, the code snippet I provided was in fact working as intended โ€“ the problem was with my terminal, and not with python encoding.

If your view needs to access a UTF-8 UploadedFile, you can just use utf8_file = codecs.EncodedFile(request.FILES['file_field'],"utf-8") to open a file object in the correct encoding.

I also noticed that, at least for InMemoryUploadedFiles, opening the file through the codecs.EncodedFile wrapper does NOT reset the seek() position of the file descriptor. To return to the beginning of the file (again, this may be InMemoryUploadedFile specific) I just used request.FILES['file_field'].open() to send the seek() position back to 0.

๐Ÿ‘คeblume

3๐Ÿ‘

I use the csv.DictReader and it appears to be working well. I attached my code snippet, but it is basically the same as another answer here.

import csv as csv_mod
import codecs

file = request.FILES['file']    
dialect = csv_mod.Sniffer().sniff(codecs.EncodedFile(file,"utf-8").read(1024))
file.open() 
csv = csv_mod.DictReader( codecs.EncodedFile(file,"utf-8"), dialect=dialect )
๐Ÿ‘คwilblack

-1๐Ÿ‘

For CSV and Excel upload to django, this site may help.

๐Ÿ‘คJohn Machin

Leave a comment