Q:

Django: uploaded file is binary. Is it possible to change to utf? So readline() returns unicode rather than bytes

Uploading a file in Django (1.7) using Python 3:
f = form.files['file']
f.__repr__()

outputs
<InMemoryUploadedFile: index.html (text/html)>

If I call f.readline() I get bytes back.
Normally that would be okay, I could just read the file and decode it, however in this case I'm passing the file on to another function that expects to call readline() on the parameter it receives, and readline() needs to return unicode rather than bytes.
Is it possible to set encoding or such on an instance of InMemoryUploadedFile, so readline would return unicode rather than bytes? Or do I have to use StringIO to first read in the entire file and then pass the instance of StringIO to my function?

A:

The general way to handle this may be to write a custom upload handler and tell Django to use it. But I've never done this, so I'm not sure.
But a simple approach would be to just wrap the underlying file object. (If you use TextIOWrapper instead of StringIO you shouldn't need to worry about the overhead.)
from io import TextIOWrapper

f = form.files['file']
text_f = TextIOWrapper(f.file, encoding='utf-8')

