UnicodeDecodeError

Peter Otten __peter__ at web.de
Sun Jul 22 14:53:43 EDT 2007


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



More information about the Python-list mailing list