On Thu, May 21, 2009 at 2:48 AM, Daniel Roseman <
roseman.dan...@googlemail.com> wrote:

>
> On May 20, 11:56 pm, Sean Brant <brant.s...@gmail.com> wrote:
> > Traceback (most recent call last):
> >[snip]
> >   File "/home/58124/containers/django/contest/apps/entries/management/
> > commands/grabentries.py", line 73, in handle
> >     print '[%s] Saved - Entry "%s"' % (datetime.now(), entry.title)
> > UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in
> > position 56: ordinal not in range(128)
> >
> > Is there any way to insure that this error does not occur? Do I need
> > to do some sort of unicode conversion with Django?
>
> Since you haven't showed us any actual code, we can't help you.


Well, we've got the line causing the exception, and the exception, and
that's probably enough code in this case.  We could use more information on
the environment.

The line in question is trying to print possibly Unicode data to a
device/file that is apparently set up to use the ascii codec.  What is
stdout here?  If it's a file, perhaps you can open it via the right codec.
Plain open() is going to use the ascii codec, which will be a problem for
any non-ascii data.  Using codecs.open() you can specify whatever encoding
you like that will be capable of representing the data you have:

>>> u = u'\2013'
>>> stderr = open('/tmp/stderr.ascii.out', 'w')
>>> print >> stderr, '%s' % u
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\x81' in position
0: ordinal not in range(128)
>>> import codecs
>>> stderr = codecs.open('/tmp/stderr.utf8.out', mode='w', encoding='utf-8')
>>> print >> stderr, '%s' % u
>>> quit()

If stdout is a device, and you can't reconfigure it to support non-ASCII
output, then you'll have to take care not to send non-ASCII data to it.  You
can print repr of the Unicode strings instead...that will be ugly but at
least it won't cause an exception.

Karen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to