【问题标题】:Gmail messages, unable to split message IdsGmail 邮件,无法拆分邮件 ID
【发布时间】:2019-03-13 19:48:32
【问题描述】:

我正在阅读 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'

【问题讨论】:

    标签: python split gmail-imap


    【解决方案1】:

    当它给你TypeError时,它不是指的是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.
    

    或者,在 python shell 的情况下

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-01
      • 2021-03-04
      • 2019-06-19
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2019-10-16
      • 1970-01-01
      相关资源
      最近更新 更多