Python Gmail邮件,无法拆分邮件ID

Python Gmail邮件,无法拆分邮件ID,python,split,gmail-imap,Python,Split,Gmail Imap,我正在阅读gmail邮件() 它抛出“TypeError:需要类似字节的对象,而不是'str'”。 但我可以清楚地看到它是一个字节对象b'6' 在pythonshell上也尝试了同样的方法,并且也得到了同样的错误。不确定这里出了什么问题 >>> b'6'.split(' ') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: a byte

我正在阅读gmail邮件()

它抛出“TypeError:需要类似字节的对象,而不是'str'”。 但我可以清楚地看到它是一个字节对象
b'6'

在pythonshell上也尝试了同样的方法,并且也得到了同样的错误。不确定这里出了什么问题

>>> b'6'.split(' ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
>>b'6'。拆分(“”)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:需要类似字节的对象,而不是“str”

当它给出
类型错误时,它不是指的是
b'6'
。。。它是指
.split()
中的
'
——它试图用字符串拆分字节对象。要解决此问题,只需将行更改为:

(retcode, messages) = conn.search(None, '(UNSEEN)')
if retcode == 'OK':
    for num in messages[0].split(b' '): # messages[0] is b'6' in my case.
或者,在pythonshell的情况下

>>> b'6'.split(b' ')
[b'6']
>>> b'6'.split(b' ')
[b'6']