[Tutor] help: UnicodeError: ASCII decoding error: ordinal not in range(1 28)

Walter Vannini walterv@jps.net
Fri, 25 May 2001 08:41:02 -0700


Hi Grzegorz,

I don't know unicode well enough to answer your question.
I hope someone out there might be able to tell you (and me) what's going on.

I can tell you that the error can be reproduced very easily via:

>>> str(u"\x81")
which attempts to convert the unicode object u"\x81" to a string object

or via:

>>> unicode("\x81")
which attempts to convert the string object "\x81" to a unicode object

Walter.

Furmanek, Greg wrote:
> 
> >>> a = "\x81"
> >>> b = "test"
> >>> c = 00123
> >>> print a b c
>   File "<stdin>", line 1
>     print a b c
>             ^
> SyntaxError: invalid syntax
> >>> print a
> 
> >>> print b
> test
> >>> print c
> 83
> >>> c = "01405'
>   File "<stdin>", line 1
>     c = "01405'
>               ^
> SyntaxError: invalid token
> >>> c = "01405"
> >>> a + b + c
> '\201test01405'
> >>> a + a + b + a + c + a + c
> '\201\201test\20101405\20101405'
> >>> u'\x81'
> u'\201'
> >>> d = u'\x81'
> >>> a + b + c + d
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> UnicodeError: ASCII decoding error: ordinal not in range(128)
> 
> I was able to recreate the error.
> 
> when I add regurlar ascii then it is ok,
> but when the unicode is added then I get
> the error.
> 
> I am using xml parsing and I guess it gives a result
> using unicode.
> 
> -> -----Original Message-----
> -> From: Walter Vannini [mailto:walterv@jps.net]
> -> Sent: Thursday, May 24, 2001 6:08 PM
> -> To: 'tutor@python.org'
> -> Subject: Re: [Tutor] help: UnicodeError: ASCII decoding
> -> error: ordinal
> -> not in range(1 28)
> ->
> ->
> -> Hi Grzegorz,
> ->
> -> I'm unable to reproduce the error message in Python 2.0.
> -> For example, the following code does not seem to trigger
> -> the error message:
> ->
> -> aString = "\x81\x82\x83"
> -> bString = "\x83\x84\x85"
> -> cString = aString+bString
> -> dString = "".join([aString, bString])
> -> import string
> -> eString = string.join([aString, bString], "")
> -> print aString
> -> print bString
> -> print cString
> -> print dString
> -> print eString
> ->
> -> Some example code would be helpful in diagnosing the problem.
> -> Also, your version of python would be useful.
> -> It can be found via "import sys; print sys.version"
> -> The string I get via this code is:
> -> "2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)]"
> ->
> -> Walter.
> ->
> -> > I am trying to manipulate string containing hex characters.
> -> >
> -> > I get following error when I try to join the strings:
> -> > UnicodeError: ASCII decoding error: ordinal not in range(128)
> -> >
> -> > How can I join strings containing "\x81" without the error?
> -> >
> -> > Grzegorz Furmanek
> -> > Furmanek.Greg@hit.cendant.com
> ->