Gmail邮件,无法拆分邮件ID

时间:2018-10-09 05:49:47

标签: python split gmail-imap

我正在阅读Gmail邮件(using the code from accepted answer

(retcode, messages) = conn.search(None, '(UNSEEN)')
if retcode == 'OK':
    for num in messages[0].split(' '): # messages[0] is b'6' in my case.

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

在python shell上也尝试过相同的操作,并且在那里也得到相同的错误。不知道这里出了什么问题。

>>> b'6'.split(' ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

1 个答案:

答案 0 :(得分:1)

当它给您TypeError时,不是指的是b'6' ...而是指{{1 }}-它试图用字符串分割一个字节对象。要解决此问题,只需将行更改为:

' '

或者,对于python shell

.split()