string encode / decode [duplicate]

2019-01-18 17:28发布

问题:

This question already has an answer here:

  • Python - email header decoding UTF-8 8 answers

'=?KOI8-R?B?W1JFUS0wMDI1NDEtNDc5NzddIO/h7yAi89TSz8rGwdLGz9IiIDs=?=\r\n\t=?KOI8-R?B?Ry43MjkgKDEwKQ==?='

How can I convert this into something readable ? Thanks !

回答1:

>>> email.header.decode_header('=?KOI8-R?B?W1JFUS0wMDI1NDEtNDc5NzddIO/h7yAi89TSz8rGwdLGz9IiIDs=?=\r\n\t=?KOI8-R?B?Ry43MjkgKDEwKQ==?=')
[('[REQ-002541-47977] \xef\xe1\xef "\xf3\xd4\xd2\xcf\xca\xc6\xc1\xd2\xc6\xcf\xd2" ;G.729 (10)', 'koi8-r')]
>>> print '[REQ-002541-47977] \xef\xe1\xef "\xf3\xd4\xd2\xcf\xca\xc6\xc1\xd2\xc6\xcf\xd2" ;G.729 (10)'.decode('koi8-r')
[REQ-002541-47977] ОАО "Стройфарфор" ;G.729 (10)


回答2:

This is encoded-word encoding as specified in RFC 2047.

The email package should be able to deal with this format.