Saturday, 15 August 2015

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



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

uploading file in django (1.7) using python 3:

f = form.files['file'] f.__repr__()

outputs

<inmemoryuploadedfile: index.html (text/html)>

if phone call f.readline() bytes back. okay, read file , decode it, in case i'm passing file on function expects phone call readline() on parameter receives, , readline() needs homecoming unicode rather bytes.

is possible set encoding or such on instance of inmemoryuploadedfile, readline homecoming unicode rather bytes? or have utilize stringio first read in entire file , pass instance of stringio function?

the general way handle may write custom upload handler , tell django utilize it. i've never done this, i'm not sure.

but simple approach wrap underlying file object. (if utilize textiowrapper instead of stringio shouldn't need worry overhead.)

from io import textiowrapper f = form.files['file'] text_f = textiowrapper(f.file, encoding='utf-8')

python django unicode

No comments:

Post a Comment