[Tutor] symbol encoding and processing problem
Evert Rol
evert.rol at gmail.com
Tue Oct 16 15:14:12 CEST 2007
Hi Tim,
> Heres my test script:
>
> 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('°')
> 10 out = raw
> 11 print out
> 12 easygui.msgbox(out)
>
> Here ist my output:
>
> 20° 12' 33''
> 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?
I don't know all the details about unicode, but here's what works for
me:
# -*- coding: utf-8 -*-
import easygui
raw = unicode("121° 55' 5.55''", 'utf-8')
print raw.encode('utf-8')
lines = raw.split(unicode('°', 'utf-8'))
print lines
easygui.enterbox(message="Enter something.", title="",
argDefaultText=raw)
So you may need to explicitly define the encoding (and encode the
degree sign in the split argument as well). Google a bit for Python
and unicode to get some more info, if you didn't do so already. (I'm
really hoping that with Python 3 all this messy stuff does go away.)
I had no problem with the easygui box, either my way or hardcoding
the string to argDefaultText as you did. Perhaps it's an underlying
problem with Tkinter, which may not support unicode on your system? A
simple test (not sure how definite that would be), is to start up a
Python cmdline, and do
>>> import Tkinter
>>> Tkinter._test()
And see if you get a ç.
Good luck,
Evert
More information about the Tutor
mailing list