在python中解码这种类型的字符串

时间:2011-06-08 18:30:33

标签: python string utf-8 imap decode

=?UTF-8 2 B 4 TmV3IFBlcnNvbmFsIE1lc3NhZ2U6IE1vbmcgYW5oIHTDrG0gbOG6oWkgY2h1eWVudGIub3JnIQ ==?=

嗨,我有这个字符串是从IMAP命令返回的 - 有人知道如何在python中解码这个,所以我会有正确的UTF-8字符串吗?

2 个答案:

答案 0 :(得分:4)

>>> print unicode(base64.decodestring('TmV3IFBlcnNvbmFsIE1lc3NhZ2U6IE1vbmcgYW5oIHTDrG0gbOG6oWkgY2h1eWVudGIub3JnIQ=='), "utf-8")
New Personal Message: Mong anh tìm lại chuyentb.org!
>>> 

答案 1 :(得分:4)

我想解码这个编码字的最佳方法是通过email.header.decode_header()。它将返回一个列表,其中第一个元素是解码后的字符串,第二个元素是编码。

示例:

email.header.decode_header('=?utf-8?Q?T=e1=ba=a1p_Ch=c3=ad_Gi=e1=ba=a3m_C=c3=a2n_=28=40tapchigiamcan=29_is_now_following_you_on_Twitter=21?=')
[('T\xe1\xba\xa1p Ch\xc3\xad Gi\xe1\xba\xa3m C\xc3\xa2n (@tapchigiamcan) is now following you on Twitter!', 'utf-8')]