Any way to tolerate Unicode w/o changing '' to u''?...

34 views
Skip to first unread message

Fred Stluka

unread,
Apr 14, 2016, 6:27:07 PM4/14/16
to django-users -- mailing list
Django users,

In my Django app, I want to allow users to enter Unicode
strings.  But if they do, I get UnicodeEncodeError when I call
format() to embed their string into other strings.  Is there an
easy way to tolerate the Unicode?

Details:

After prompting the user for name, I may have a line of code like:

    log('Name entered was: {0}'.format(name))

With Python 2.7.3 and Django 1.4.3, this raises exception
UnicodeEncodeError if the user entered a string containing
Unicode chars, because it tries to embed Unicode chars into
my ASCII string template.  So, I have to I change it to:

    log(u'Name entered was: {0}'.format(name))

I have many thousands of such lines in my 200,000+ lines of
Python and Django code.

Is there a master switch somewhere that would cause a Python
2.x string literal to default to Unicode instead of ASCII, as it
does in Python 3? 

Or do I have to explicitly change '' to u'' in all such places?

I looked into adding a line like this to the top of the file, but
no luck because it seems to only affect the encoding of Unicode
string literals (u''), not regular string literals (''):

    # -*- coding: utf-8 -*-

Any suggestions?  Much appreciated.  Thanks!

--Fred
Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.

Stephen J. Butler

unread,
Apr 14, 2016, 6:32:36 PM4/14/16
to django...@googlegroups.com
from __future__ import unicode_literals


--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/57101921.80505%40bristle.com.
For more options, visit https://groups.google.com/d/optout.

Fred Stluka

unread,
Apr 14, 2016, 6:42:14 PM4/14/16
to django...@googlegroups.com
Stephen,

Perfect!  Thanks!!!!!


--Fred
Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.

Tim Graham

unread,
Apr 14, 2016, 10:05:55 PM4/14/16
to Django users
You want `from __future__ import unicode_literals`: https://docs.djangoproject.com/en/dev/ref/unicode/#general-string-handling

Fred Stluka

unread,
Apr 20, 2016, 11:41:47 PM4/20/16
to django...@googlegroups.com
Tim,

Excellent response, thanks!  The link to the doc is just what I
needed.

One more question.  I'm still getting UnicodeEncodeError on
some calls to str() because I should now be calling unicode()
instead.  Any easy way to map all of my str() calls to unicode()
call?

Thanks!
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Tim Graham

unread,
Apr 21, 2016, 7:54:36 AM4/21/16
to Django users
I guess you might want six.text_type. https://docs.djangoproject.com/en/dev/topics/python3/#string-handling may help.


On Wednesday, April 20, 2016 at 11:41:47 PM UTC-4, Fred Stluka wrote:
Tim,

Excellent response, thanks!  The link to the doc is just what I
needed.

One more question.  I'm still getting UnicodeEncodeError on
some calls to str() because I should now be calling unicode()
instead.  Any easy way to map all of my str() calls to unicode()
call?

Thanks!
--Fred
Reply all
Reply to author
Forward
0 new messages