How to convert email subject from "? UTF-8? ...? =" To readable string?

Possible duplicate:
encoding / decoding string

Now the object looks like this: =? UTF-8? B 0J / RgNC + 0LLQtdGA0LrQsA ==? =

+4
source share
2 answers

The part between =?UTF-8?B? and ?= is a base64 encoded string. Extract this part, and then decrypt it.

 import base64 #My buggy SSH account needs this to write unicode output, you hopefully won't import sys import codecs sys.stdout = codecs.getwriter('utf-8')(sys.stdout) encoded = '=?UTF-8?B?0J/RgNC+0LLQtdGA0LrQsA==?=' prefix = '=?UTF-8?B?' suffix = '?=' #extract the data part of the string middle = encoded[len(prefix):len(encoded)-len(suffix)] print "Middle: %s" % middle #decode the bytes decoded = base64.b64decode(middle) #decode the utf-8 decoded = unicode(decoded, 'utf8') print "Decoded: %s" % decoded 

Output:

 Middle: 0J/RgNC+0LLQtdGA0LrQsA== Decoded:  
+9
source
+10
source

Source: https://habr.com/ru/post/1343171/


All Articles