Re: UnicodeDecodeError

Discussion in 'Python' started by Ken Seehart, Jul 21, 2007.

  1. Ken Seehart

    Ken Seehart Guest

    Um, never mind. The recent unicode conversation gave me my answer :)
    unicode(s, 'Windows-1252')

    Ken Seehart wrote:
    > I get this whenever I encounter a non-ascii character in a non-unicode
    > string:
    >
    > UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 23:
    > ordinal not in range(128)
    >
    > The string in question is "... ESPA\xd1OL ..."
    >
    > I completely understand why I get the error, and my solution will be to
    > simply convert to unicode (since the code that uses the string is
    > unicode ready).
    >
    > I am wondering if anyone knows where I can find a mapping from this
    > particular extended ascii code (where \xd1 is Ñ), to the corresponding
    > unicode characters.
    >
    > Ken
    >
    >
    >
     
    Ken Seehart, Jul 21, 2007
    #1
    1. Advertising

  2. Ken Seehart

    Peter Otten Guest

    Ken Seehart wrote:

    >> I am wondering if anyone knows where I can find a mapping from this
    >> particular extended ascii code (where \xd1 is Ñ), to the corresponding
    >> unicode characters.


    > Um, never mind. The recent unicode conversation gave me my answer :)
    > unicode(s, 'Windows-1252')


    Run the following script to get a few more candidates:

    import encodings
    import os
    import glob

    def encodings_from_modulenames():
    ef = os.path.dirname(encodings.__file__)
    for fn in glob.glob(os.path.join(ef, "*.py")):
    fn = os.path.basename(fn)
    yield os.path.splitext(fn)[0]

    def find_encodings(unistr, bytestr, encodings=None):
    if encodings is None:
    encodings = encodings_from_modulenames()
    for encoding in encodings:
    try:
    encoded = unistr.encode(encoding)
    except Exception:
    pass
    else:
    if encoded == bytestr:
    yield encoding

    for encoding in find_encodings(u"\N{LATIN CAPITAL LETTER N WITH TILDE}", "\xd1"):
    print encoding

    Peter
     
    Peter Otten, Jul 22, 2007
    #2
    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. Ruslan
    Replies:
    1
    Views:
    520
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Sep 7, 2004
  2. Robin Siebler
    Replies:
    4
    Views:
    27,457
    Tim Peters
    Oct 8, 2004
  3. Thomas Thomas

    UnicodeDecodeError

    Thomas Thomas, May 5, 2005, in forum: Python
    Replies:
    2
    Views:
    330
    Michael Spencer
    May 5, 2005
  4. F. GEIGER
    Replies:
    0
    Views:
    1,611
    F. GEIGER
    May 27, 2005
  5. ash

    UnicodeDecodeError

    ash, Nov 30, 2005, in forum: Python
    Replies:
    5
    Views:
    491
Loading...

Share This Page