UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 10: ordinal not in range(128)

Discussion in 'Python' started by Robin Siebler, Oct 8, 2004.

  1. I have no idea what is causing this error, or how to fix it. The full error is:

    Traceback (most recent call last):
    File "D:\ScriptRuntime\PS\Automation\Handlers\SCMTestToolResourceToolsBAT.py",
    line 60, in Run
    PS.Automation.Utility.System.AppendSystemPath(args["PATH"], context)
    File "D:\ScriptRuntime\PS\Automation\Utility\System.py", line 55, in AppendSys
    temPath
    AppendPathVariable("PATH", appendtext, context)
    File "D:\ScriptRuntime\PS\Automation\Utility\System.py", line 37, in AppendPat
    hVariable
    if(ap == pp):
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 10: ordinal
    not in range(128)

    The code for the function is:

    def AppendPathVariable(variable, appendtext, context):
    """AppendSystemPath(appendtext, context) -> None

    Appends a directory string to the system path. The string can be
    as single path or multiple paths seperated by a semi-colon."""

    if(os.environ.has_key(variable)):
    curpath = os.environ[variable]

    pathparts = string.split(curpath, ";")
    appendparts = string.split(appendtext, ";")

    for ap in appendparts:
    found = 0

    for pp in pathparts:
    if(ap == pp):
    found = 1

    if(found == 0):
    pathparts.append(ap)
    #end for ap in appendparts

    newpath = string.join(pathparts, ";")
    os.environ[variable] = newpath
    else:
    os.environ[variable] = appendtext
     
    Robin Siebler, Oct 8, 2004
    #1
    1. Advertising

  2. Robin Siebler

    Jeff Epler Guest

    Re: UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in

    If you compare a unicode string to a byte string, and the byte-string
    has byte values >127, you will get an error like this:
    >>> u'a' == '\xc0'

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0: ordinal not in range(128)

    There is no sensible way for Python to perform this comparison, because
    the byte string '\xc0' could be in any encoding. If the encoding of the
    byte string is latin-1, it's LATIN CAPITAL LETTER A WITH GRAVE. If it's
    koi8-r encoded, it's CRYILLIC SMALL LETTER YU. Python refuses to guess
    in this case.

    It doesn't matter whether the unicode string contains any characters
    that are non-ASCII characters.

    To correct your function, you'll have to know what encoding the byte
    string is in, and convert it to unicode using the decode() method,
    and compare that result to the unicode string.

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.6 (GNU/Linux)

    iD8DBQFBZdclJd01MZaTXX0RAn3dAJ0SnEr4Rc841EZlZqeDVnLl5khIvACfcaUz
    pym81hgmHf6yv592fGPEw7c=
    =HYB+
    -----END PGP SIGNATURE-----
     
    Jeff Epler, Oct 8, 2004
    #2
    1. Advertising

  3. Re: UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 10: ordinal not in range(128)

    It's just a file path. It doesn't *have* any non-ASCII chars in it to
    begin with! That's why I don't understand why I'm getting the error.
    I didn't get it with Python 2.2, but we just upgraded to
    (Active)Python 2.3.
     
    Robin Siebler, Oct 8, 2004
    #3
  4. Robin Siebler

    Jeff Epler Guest

    Re: UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in

    On Fri, Oct 08, 2004 at 12:25:26PM -0700, Robin Siebler wrote:
    > It's just a file path. It doesn't *have* any non-ASCII chars in it to
    > begin with!


    Well, then, be sure to follow up when you find the real cause, because I
    don't know of another reason that isn't along the lines I mentioned.

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (GNU/Linux)

    iD8DBQFBZu25Jd01MZaTXX0RAr4HAJ4p9Isl8/ZGxhZhAWLU39yYszd+WwCfSm4e
    A8Lbqnb270taGwlfoE6P3FA=
    =kwqz
    -----END PGP SIGNATURE-----
     
    Jeff Epler, Oct 8, 2004
    #4
  5. Robin Siebler

    Tim Peters Guest

    Re: UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in

    [Robin Siebler]
    >>> It's just a file path. It doesn't *have* any non-ASCII chars in it to
    >>> begin with!


    [Jeff Epler]
    >> Well, then, be sure to follow up when you find the real cause, because I
    >> don't know of another reason that isn't along the lines I mentioned.


    Robin, your original report disagrees with your belief:

    > if(ap == pp):
    > UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 10: ordinal
    > not in range(128)


    0xa0 is not an ASCII character. We can't tell from the traceback
    which of ap and pp is Unicode, and which isn't, but presumably your
    knowledge of your app will tell you.

    That something is "a file path" doesn't mean anything -- you're
    running on Windows, and Windows doesn't restrict paths to containing
    ASCII characters.

    >>> f = open('\xa0\xa0.txt', 'w')
    >>> f.name

    '\xa0\xa0.txt'
    >>> f.close()
    >>> import os
    >>> for fn in os.listdir('.'):

    .... if fn.endswith('.txt'):
    .... print fn
    b.txt
    bb.txt
    BUILDno.txt
    NormalizationTest-3.2.0.txt
    readme.txt
    รกรก.txt

    The last line may not show up correctly for you, since it contains
    non-ASCII characters. When I sent it, it looked like

    aa.txt

    but with diacritcal marks "on top of" the a's.
     
    Tim Peters, Oct 8, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oleg  Parashchenko
    Replies:
    4
    Views:
    1,073
    Paul Boddie
    Mar 31, 2007
  2. Gilles Ganault
    Replies:
    2
    Views:
    1,153
    Gilles Ganault
    Jun 17, 2008
  3. Gabriel Genellina
    Replies:
    0
    Views:
    782
    Gabriel Genellina
    Oct 21, 2008
  4. Gilles Ganault
    Replies:
    3
    Views:
    1,985
    Steve Holden
    Oct 29, 2008
  5. Tim Golden
    Replies:
    0
    Views:
    129
    Tim Golden
    Nov 27, 2013
Loading...

Share This Page