2

=?UTF-8?B?TmV3IFBlcnNvbmFsIE1lc3NhZ2U6IE1vbmcgYW5oIHTDrG0gbOG6oWkgY2h1eWVudGIub3JnIQ==?=

Hi, I have this string which is what returned from an IMAP command - do anyone know how to decode this in python so I'll have the proper UTF-8 string?

2 Answers 2

4
>>> print unicode(base64.decodestring('TmV3IFBlcnNvbmFsIE1lc3NhZ2U6IE1vbmcgYW5oIHTDrG0gbOG6oWkgY2h1eWVudGIub3JnIQ=='), "utf-8")
New Personal Message: Mong anh tìm lại chuyentb.org!
>>> 
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks! how can I automatically split the original string to get the two components? and use something rather than unicode() if the first parameter in the string is not 'UTF-8'?
for example: =?ISO-8859-1?B?UmU6IENo4G8gYuFj?=
print unicode(base64.decodestring('UmU6IENo4G8gYuFj'), "8859")
To completely answer your question, im not sure if there is a function to find the char set for you. You might just have to make a lookup table?
@Thi - I found the codec list if you have to make your own lookup table- docs.python.org/library/codecs.html#standard-encodings
|
4

I guess the best way to decode this Encoded-Word is via email.header.decode_header(). It'll return you a list in which the first element is the decoded string, and the second element is the encoding.

Example:

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')]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.