Python IMAP: =?utf-8?Q? in subject string

前端 未结 5 957
忘掉有多难
忘掉有多难 2020-12-17 10:36

I am displaying new email with IMAP, and everything looks fine, except for one message subject shows as:

=?utf-8?Q?Subject?=

How ca

5条回答
  •  时光取名叫无心
    2020-12-17 11:13

    This is a MIME encoded-word. You can parse it with email.header:

    import email.header
    
    def decode_mime_words(s):
        return u''.join(
            word.decode(encoding or 'utf8') if isinstance(word, bytes) else word
            for word, encoding in email.header.decode_header(s))
    
    print(decode_mime_words(u'=?utf-8?Q?Subject=c3=a4?=X=?utf-8?Q?=c3=bc?='))
    

提交回复
热议问题