[Grok-dev] Re: Understanding unicode

Martijn Faassen faassen at startifact.com
Fri Aug 31 14:40:28 EDT 2007


Jan Ulrich Hasecke wrote:
> Hi,
> 
> I use a Choice in grok.Fields.
> 
> form_fields = grok.Fields(
>     name=Choice(title=u'Art', values=[u'Elefantengehege', 
> u'Giraffengehege', u'Paviankaefig'])
> 
> When I insert u'Paviankäfig' I get an unicode error.
> 
>  UnicodeEncodeError: 'ascii' codec can't encode characters in position 
> 7-8: ordinal not in range(128)
> 
> I used name=Affenkäfig before though.

It would be helpful if you gave a longer traceback so we can see where 
in the code you get the unicode error. Do you get this during runtime or 
during startup time?

It is possible that you haven't set the source-code encoding of your 
Python file. Then again, this typically gives you a different kind of error:

   SyntaxError: Non-ASCII character '\xc3' in file test.py on line 1, 
but
   no encoding declared; see http://www.python.org/peps/pep-0263.html for
   details

that was a test with python 2.5. python2.4 gives me the following 
slightly weird message:

   sys:1: DeprecationWarning: Non-ASCII character '\xc3' in file test.py
   on line 1, but no encoding declared; see
   http://www.python.org/peps/pep-0263.html for details

in that case, you can control the coding by adding a line like this to 
the top of your file:

# -*- coding: UTF-8 -*-

I recommend using UTF-8 whenever an encoding is needed, though using 
another encoding such as latin-1 should be fine for source file encodings.

Regards,

Martijn



More information about the Grok-dev mailing list