2017-05-06 101 views
1

在Python解碼引用打印字符串,我得到了Quoted-Printable encoding編碼問題:在Python

mystring="=AC=E9" 

編碼的字符串,該字符串應該打印爲

é

所以我想解碼它並用UTF-8編碼,我猜。我知道有什麼可以通過

import quopri 
quopri.decodestring('=A3=E9') 

但是,我完全失去了。你將如何解碼/編碼該字符串以正確打印?

回答

1

試試這個。

import quopri 
mystring="=AC=E9" 
decoded_string=quopri.decodestring(mystring) 
print(decoded_string.decode('windows-1251')) 
+0

不幸的是,我以前試過,但它看起來窗口-1251是專爲編碼俄語。當我運行你的代碼塊時,我得到一個打印的信息。這不是它看起來的樣子。 'é' –

1

好球員,我不知道爲什麼,但這個功能似乎工作:

from email.parser import Parser 

def decode_email(msg_str): 
    p = Parser() 
    message = p.parsestr(msg_str) 
    decoded_message = '' 
    for part in message.walk(): 
     charset = part.get_content_charset() 
     if part.get_content_type() == 'text/plain': 
      part_str = part.get_payload(decode=1) 
      decoded_message += part_str.decode(charset) 
    return decoded_message