views:

160

answers:

1

In my Django webapp, in one location users can upload a text file where each line contains a string which will be operated on - the file isn't being stored on the server or anything like that.

My code looks like this:

roFile = request.FILES['uploadFileName']
ros = roFile.read().strip()
ros = ros.split('\n')
ros = [t.strip() for t in ros]

To date, this has worked AOK. Today I had a user uploading a file which was causing issues. Using these strings in Django generates the following error:

ProgrammingError: ERROR: invalid byte sequence for encoding "UTF8":0xff

The user has told me that he saved the file as UTF-16.

Within python proper, I can do the following:

import codecs
from django.utils.encoding import *
fo = codecs.open('filename', 'r', 'utf-16')
zz = fo.readlines()

and then the values seem to be manageable, but not with the file upload.

What is the appropriate way to deal with the data in request.FILES in order to handle the differing character set?

+1  A: 

This first part doesn't answer your question (I know nothing about django); I'd just like to point out that when you supply code that you say works or doesn't work, you should copy/past the actual code that you ran,; don't type it from memory.

This code:

import codecs
from django.utils.encoding
f = codecs.open('filename', 'r', 'utf-16')
zz = fo.readlines()

has 2 problems and looks like it should be:

import codecs from django.utils.encoding
fo = codecs.open('filename', 'r', 'utf-16')
zz = fo.readlines()

To your question: google("django request files") seems to give some useful clues; have you investigated them, including this? One of the clues was that file uploads seem to have been improved in later versions of django; what version are you using?

John Machin
Thanks. Yes, the lack of copy/paste was due to using two unconnected machines at the time - you're right that there's an error but not quite on with the fix, I'll edit my post to reflect.I did see that SO page, I realized after I left that when I tried using the answer which most likely would solve the problem that I had things setup slightly differently which would prevent it from working. The exact code I had evolved a bit yesterday as I was tryign to get this working.
geoffjentry
Yep, I was right in that I had been misapplying the examples in the older post. Working now ....
geoffjentry
For the benefit of others who may have this problem, consider posting what your solution turned out to be. Do mention what version(s) of django it works with.
John Machin