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.
On Tuesday, May 19, 2015 at 3:24:39 AM UTC-6, HM wrote: > > 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. > -- 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 post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/856f986a-4ee2-4f84-9dc0-82c131de36d4%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.