【问题标题】:Gmail retrive body text cwoth out b'..\r\n exmaple b'message1\r\n'Gmail 检索正文文本 cwoth out b'..\r\n exmaple b'message1\r\n'
【发布时间】:2021-02-05 17:05:06
【问题描述】:

通过这个脚本,我想从 gmdail 中检索消息。

脚本的预期输出应该是message1, message2, message3, message4, message5

但是脚本打印出以下列表[b'message1\r\n', b'message2\r\n', b'message3\r\n', b'message4\r\n', b'message5\r\n']

有人可以帮助我吗? 这是代码sn-p:

import imaplib

def read_gmail():
    # user and pass for login to gmail server
    username = input("Enter Email Address for Login: ").lower()
    password = input("Enter password for Login: ").lower()

    mail = imaplib.IMAP4_SSL('imap.gmail.com', 993)

    if username == "me" and password == "me":
        mail.login('mymail', 'mypass')
    else:
        mail.login(username, password)

    mail.list()
    mail.select("Inbox")

    status, data = mail.search(None, 'SUBJECT "Enc Message"')  # all message with Subject-> Enc Message
    minimata = [] 
    clear_lista = []

    for num in data[0].split():
        status, data = mail.fetch(num, '(BODY.PEEK[TEXT])')  # to see the body text
        
        minimata.append(data[0][1])  # apothikeyo se nea lista ta kryprografimena minimata.
        
    for ch in minimata:
        clear_lista.append(ch)

    return clear_lista

【问题讨论】:

    标签: python list email gmail fetch


    【解决方案1】:

    目前,您的 minimata 列表包含字节对象,这就是前导 b' 的来源。要摆脱它们,您只需要解码它们。

    关于\n\r,可以使用rstrip()


    以下应该可以解决问题:

    for ch in minimata:
        clear_lista.append(ch.rstrip().decode())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-06
      • 2011-09-18
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 2011-09-26
      相关资源
      最近更新 更多