Pan*_*Jee 3 python encoding internationalization
我试图使用Python脚本(Python 2.5和PyPy)阅读我的电子邮件我的一些结果不是ASCII,我得到这样的字符串:
=?ISO-8859-7 2 B 4 0OXm7/Dv8d/hIPP07 + 0gyuno4enx/u3h?="
有没有办法解码它并转换为utf-8,以便我可以处理它?我试过.decode('ISO-8859-7'),但我得到了相同的字符串
import email.header as eh
unicode_data= u''.join(
str_data.decode(codec or 'ascii')
for str_data, codec
in eh.decode_header('=?ISO-8859-7?B?0OXm7/Dv8d/hIPP07+0gyuno4enx/u3h?='))
# unicode_data now is u'????????? ???? ?????????'
Run Code Online (Sandbox Code Playgroud)
你应该在这里使用unicode_data.但是,如果您(认为您)需要UTF-8编码的字符串,您可以:
utf8data= unicode_data.encode('utf-8')
Run Code Online (Sandbox Code Playgroud)
更新:我改变了.decode通话,以应付情况下codec是None(例如eh.decode_header('plain text'))