How to convert between Japanese coding systems?

Justin Ezequiel justin.mailinglists at gmail.com
Thu Feb 19 05:12:18 EST 2009


import email
from email.Header import decode_header
from unicodedata import name as un

MS = '''\
Subject: =?UTF-8?Q?
romaji=E3=81=B2=E3=82=89=E3=81=8C=E3=81=AA=E3=82=AB=E3=82=BF?=
Date: Thu, 19 Feb 2009 09:34:56 -0000
MIME-Version: 1.0
Content-Type: text/plain; charset=EUC-JP
Content-Transfer-Encoding: base64

cm9tYWpppNKk6aSspMqlq6W/paulyrTBu/oNCg0K
'''

def get_header(msg, name):
    (value, charset), = decode_header(msg.get(name))
    if not charset: return value
    return value.decode(charset)

if __name__ == '__main__':
    msg = email.message_from_string(MS)
    s = get_header(msg, 'Subject')
    print repr(s)
    for c in s:
        try: print un(c)
        except ValueError: print repr(c)
    print

    e = msg.get_content_charset()
    b = msg.get_payload(decode=True).decode(e)
    print repr(b)
    for c in b:
        try: print un(c)
        except ValueError: print repr(c)
    print



More information about the Python-list mailing list