5

Possible Duplicate:
string encode / decode

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

Community
  • 1
  • 1
anton
  • 61
  • 1
  • 1
  • 3

2 Answers2

11

Maybe you can use decode_header function: http://docs.python.org/library/email.header.html#email.header.decode_header

gruszczy
  • 40,948
  • 31
  • 128
  • 181
  • Thanks for replying, but result is not good: [('\xd0\x9f\xd1\x80\xd0\xbe\xd0\xb2\xd0\xb5\xd1\x80\xd0\xba\xd0\xb0', 'utf-8')] – anton Mar 10 '11 at 12:37
  • 1
    You can convert that result into a unicode string, by using `unicode(*result[0])`. – gnud Mar 10 '11 at 13:00
11

The part between =?UTF-8?B? and ?= is a base64-encoded string. Extract that part, and then decode 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: Проверка
gnud
  • 77,584
  • 5
  • 64
  • 78