Python 3 imaplib.fetch TypeError:无法将字节连接为int [英] Python 3 imaplib.fetch TypeError: can't concat bytes to int

查看:68
本文介绍了Python 3 imaplib.fetch TypeError:无法将字节连接为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码可以提取IMAP电子邮件,并且在Python 2中完全可以正常工作.在Python3中,出现以下错误:

I have some code which fetches IMAP emails and works completely well in Python 2. In Python3 I get the following error:

回溯(最近通话最近):

中的文件"./mail.py",第295行 item = return_message(x)
在return_message
中的文件"./mail.py",第122行 结果,数据= mail.fetch(message_id,(RFC822)")
提取中的文件"/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py",第460行,
typ,dat = self._simple_command(名称,message_set,message_parts)
_simple_command
中的文件"/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py",第1113行 返回self._command_complete(名称,self._command(名称,* args))
_command
中的文件"/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py",第883行 数据=数据+ b''+ arg
TypeError:无法将字节连接到int

Traceback (most recent call last):
File "./mail.py", line 295, in
item=return_message(x)
File "./mail.py", line 122, in return_message
result, data = mail.fetch(message_id, "(RFC822)")
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 460, in fetch
typ, dat = self._simple_command(name, message_set, message_parts)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1113, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 883, in _command
data = data + b' ' + arg
TypeError: can't concat bytes to int

return_message函数中的代码:

The code from the return_message function:

result, data = mail.fetch(message_id, "(RFC822)")
raw_email = data[0][1]
email_message = email.message_from_string(raw_email)

运行时信息:

3.3.0(v3.3.0:bd8afb90ebf2,2012年9月29日,01:25:11)
[GCC 4.2.1(Apple Inc.版本5666)(第3点)]

3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 01:25:11)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]

推荐答案

message_set是字符串,而不是整数.从文档:

message_set is a string, not an int. From the documentation:

下面命令的message_set选项是一个字符串,指定要执行的一条或多条消息.它可以是一个简单的消息号('1'),一个消息号范围('2:4')或一组用逗号分隔的不连续范围('1:3,6:9').范围可以包含一个星号,以表示无限上限('3:*').

The message_set options to commands below is a string specifying one or more messages to be acted upon. It may be a simple message number ('1'), a range of message numbers ('2:4'), or a group of non-contiguous ranges separated by commas ('1:3,6:9'). A range can contain an asterisk to indicate an infinite upper bound ('3:*').

直接将其转换为字符串应该足够:

Converting it to string directly should be enough:

result, data = mail.fetch(str(message_id), "(RFC822)")

这篇关于Python 3 imaplib.fetch TypeError:无法将字节连接为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆