Converting bytes to unicode that works both in python2 and 3

42 views
Skip to first unread message

Hanne Moa

unread,
May 19, 2015, 5:24:39 AM5/19/15
to django...@googlegroups.com
I got to start a new django 1.8-project in python 3.4 for once, and happily coded away. Turns out, the PaaS it needs to run on has a broken python 3.4, so... my code now needs to work in 2.7.8.

Everything works in both versions except I need to read from an uploaded file, bot in the web and from the commandline. In the management command I can do:

import io
with io.open(fieldname, 'rt', encoding='UTF-8') as F: do_stuff(F)

... and it works in both 2.7.8 and 3.4.x.

But a FileField in Python 3 reads stuff as bytes. So in the form, I need to check whether the chunk of data I get is bytes, and if it is, convert it to unicode from 'UTF-8'.

How to do that? It'll probably involve six in some way but the docs for six, and django's "how to have things run on both python 2 and 3"-docs, could be better.

Vernon D. Cole

unread,
May 19, 2015, 10:53:31 AM5/19/15
to django...@googlegroups.com
if sys.version_info >= (3,0):
   
def str2bytes(sval):
       
return sval.encode("latin1")
    unicode
= str
else:
   
def str2bytes(sval):
       
if isinstance(sval, str):
           
return sval
       
return sval.encode("latin1")
This is what I use to solve the opposite problem.  It should give you an idea how to go about it.
Reply all
Reply to author
Forward
0 new messages