[Tutor] symbol encoding and processing problem

Kent Johnson kent37 at tds.net
Tue Oct 16 14:19:28 CEST 2007


Tim Michelsen wrote:
> Dear list,
> I have encountered a problem with encoding of user input and variables.

> Heres my test script:

Posting without the line numbers would make it easier to try your code.
> 
>  1 #!/usr/bin/env python
>  2 # -*- coding: utf-8 -*-
>  3 from easygui import easygui
>  4 import sys
>  5 #raw = sys.argv[1]
>  6 raw = easygui.enterbox(message="Enter something.", title="",
> argDefaultText="20° 12' 33''")
>  7 #unicode = unicode(raw)
>  8 #conv = raw.encoding('latin-1')
>  9 split = raw.split('°')

try a unicode string: split = raw.split(u'°')
> 10 out = raw
> 11 print out
> 12 easygui.msgbox(out)
> 
> Here ist my output:
> 
> 20° 12' 33''

How do you get this output? The print is after the statement causing the 
traceback. Are you showing the same code as you ran?

> Traceback (most recent call last):
>   File "C:\python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
>     exec codeObject in __main__.__dict__
>   File "D:\python\scripts\encoding-test.py", line 22, in ?
>     split = raw.split('°')
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 0: ordinal
> not in range(128)
> Traceback (most recent call last):
>   File "C:\python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
>     exec codeObject in __main__.__dict__
>   File "D:\python\scripts\encoding-test.py", line 22, in ?
>     split = raw.split('°')
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 0: ordinal
> not in range(128)
> 
> Therefore my question:
> * How can I split at the "°" without getting a charater encoding error?
> * How do I have to encode the "°"-symbol that it gets correctly displayed in the
> Easygui msgbox at line 6?

It displays correctly for me (on MacOS X). Are you sure your source is 
actually encoded in utf-8? What platform are you on?

Kent


More information about the Tutor mailing list