Python for Vcard Parsing in UTF16

Adam Atlas adam at
Tue Apr 24 13:54:37 CEST 2007

On Apr 21, 7:28 pm, R Wood <r... at> wrote:
> I know nothing about Python except that it interests me and has interested me
> since I first learned the Rekall database frontend (Linux) runs on it.  I just
> ordered Learning Python and if that works out satisfactorily I'm going to go
> back for Programming Python.  In the meantime, I thought I would pose the
> question to this newsgroup: would Python be useful for a parsing exercise like
> this one?

Here's a little function that takes some `str`-type data (i.e. what
you'd get from doing open(...).read()) and, assuming it's a Vcard,
detects its encoding and converts it to a canonical `unicode` object.

def fix_encoding(s):
    m = u'BEGIN:VCARD'
    for c in ('ascii', 'utf_16_be', 'utf_16_le', 'utf_8'):
        try: u = unicode(s, c)
        except UnicodeDecodeError: continue
        if m in u: return u
    return None

More information about the Python-list mailing list