Hi. First of all, I think it's impossible to perfectly detect encoding 
without further information. See the answer in this SO post: 
https://stackoverflow.com/questions/436220/how-to-determine-the-encoding-of-text
 There 
are many packages and tools to help detect encoding format, but keep in 
mind that they are only giving educated guesses. (Most of the time, the 
guess is correct, but do check the dev page to see whether there are known 
issues related to your problem.)

Now let's say you have decided to use chardet. Check its doc page for the 
usage: https://chardet.readthedocs.io/en/latest/usage.html#usage You'll 
have more than one solutions. Here are some examples:

1. If the files uploaded to your server are all expected to be small csv 
files (less than a few MB and not many users do it concurrently), you can 
do the following:

#in the view to handle the uploaded file: (assume file input name is just 
"file")
file_content = request.FILES['file'].read()
chardet.detect(file_content)

2. Also, chardet seems to support incremental (line-by-line) detection 
https://chardet.readthedocs.io/en/latest/usage.html#example-detecting-encoding-incrementally

Given this, we can also read from requests.FILES line by line and pass each 
line to chardet

from chardet.universaldetector import UniversalDetector

#somewhere in a view function
detector = UniversalDetector()
file_handle = request.FILES['file']
for line in file_handle:
    detector.feed(line)
    if detector.done: break
detector.close()
# result available as a dict at detector.result





On Tuesday, July 21, 2020 at 7:09:35 AM UTC+8, Ronaldo Mata wrote:
>
> How to deal with encoding when you try to read a csv file on view.
>
> I have a view to upload csv file, in this view I read file and save each 
> row as new record.
>
> My bug is when I try to upload a csv file with a differente encoding (not 
> UTF-8)
>
> how to handle this on django (using request.FILES) I was researching and I 
> found chardet but I don't know how to pass it a request.FILES. I need help 
> please.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/64307441-0e65-45a2-b917-ece15a4ea729o%40googlegroups.com.

Reply via email to