无法将字节转换为int Python

时间:2015-03-09 09:01:29

标签: python-3.x

我刚刚开始使用Python并遇到错误 - TypeError:无法将字节连接到int

以下是代码:

import imaplib
imaplib._MAXLINE = 200000
import email

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('XXXXgmail.com', 'XXXX')
mail.list()
mail.select('inbox')
typ, data = mail.search(None, 'ALL')
ids = data[0]
id_list = ids.split()

#get the most recent email id
latest_email_id = int(id_list[-1])

#iterate through 15 messages in descending order starting with latest_email_id
#the '-1' dictates reverse looping order
for i in range(latest_email_id, latest_email_id - 15, -1):
    typ, data = mail.fetch(i, '(RFC822)')

    for response_part in data:
        if isinstance(response_part, tuple):
            msg = email.message_from_string(response_part[1])
            varSubject = msg['subject']
            varFrom = msg['from']

    #remove the brackets around the sender email address
    varFrom = varFrom.replace('<', '')
    varFrom = varFrom.replace('>', '')

    #add ellipsis (...) if subject length is greater than 35 characters
    if len(varSubject) > 35:
        varSubject = varSubject[0:32] + '...'

    print('[' + varFrom.split()[-1] + ']' + varSubject)
mail.close()

这是堆栈跟踪:

C:\Python34\python.exe C:/Users/basse_000/PycharmProjects/ReadEmail/ReadEmail.py
Traceback (most recent call last):
  File "C:/Users/basse_000/PycharmProjects/ReadEmail/ReadEmail.py", line 23, in <module>
    typ, data = mail.fetch(i, '(RFC822)')
  File "C:\Python34\lib\imaplib.py", line 474, in fetch
    typ, dat = self._simple_command(name, message_set, message_parts)
  File "C:\Python34\lib\imaplib.py", line 1134, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Python34\lib\imaplib.py", line 899, in _command
    data = data + b' ' + arg
TypeError: can't concat bytes to int

Process finished with exit code 1

我知道问题是什么,但不知道如何修复它。我也试过这个例子 - Python 3 imaplib.fetch TypeError: can't concat bytes to int

但是当我尝试修复它时遇到了这个错误:

TypeError:initial_value必须是str或None,而不是bytes

那么如何将数据转换为我需要的数据呢?

0 个答案:

没有答案