[Tutor] help: UnicodeError: ASCII decoding error: ordinal no t in range(1 28)
D-Man
dsh8290@rit.edu
Fri, 25 May 2001 13:16:16 -0400
On Fri, May 25, 2001 at 12:19:11PM -0400, Furmanek, Greg wrote:
| Ok I found the answer.
|
| When I read the unicode I try to convert it to
| ascii by:
|
| ascii_string = u'\x81'.encode('ascii')
Are you sure it works?
Python 2.1 (#1, Apr 17 2001, 09:45:01)
[GCC 2.95.3-2 (cygwin special)] on cygwin_nt-4.01
Type "copyright", "credits" or "license" for more information.
>>> u'\x81'.encode( 'ascii' )
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeError: ASCII encoding error: ordinal not in range(128)
>>> '\x81'
'\x81'
>>> u'\x81'
u'\x81'
>>>
I don't know much about unicode, other than it is a different map from
binary <-> characters than ascii is.
My question(s) for you: Do you really need to use unicode? In the
above example you have a string literal. In that case, why not use an
ascii string literal? Alternatively can you convert the ascii strings
that you have to unicode?
>>> u'\x81' + "Hello"
u'\x81Hello'
>>> "Hello" + u'\x81'
u'Hello\x81'
I'm noticing that here the ascii string was automatically promoted to
a unicode string (kind of like int->float or int->long promotion).
-D