gpt4 book ai didi

python - 格式化程序有问题 : "unsupported operand type(s) for %: ' NoneType' and 'tuple' "

转载 作者:行者123 更新时间:2023-11-28 20:04:36 26 4
gpt4 key购买 nike

我收到此错误,但我不明白为什么。

我正在学习使用 Gmail 的 API 并从以下位置复制粘贴他们的示例:https://developers.google.com/gmail/api/v1/reference/users/threads/get#examples

代码如下:

def GetThread(service, user_id, thread_id):
"""Get a Thread.

Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
thread_id: The ID of the Thread required.

Returns:
Thread with matching ID.
"""
try:
thread = service.users().threads().get(userId=user_id, id=thread_id).execute()
messages = thread['messages']
print ('thread id: %s - number of messages '
'in this thread: %d') % (thread['id'], len(messages))
return thread
except errors.HttpError, error:
print 'An error occurred: %s' % error

我目前遇到这个错误:

    thread id: %s - number of messages in this thread: %d
Traceback (most recent call last):
File "quickstart1.py", line 176, in <module>
main()
File "quickstart1.py", line 152, in main
GetThread(service, EMAIL_LOGIN, thread_id)
File "quickstart1.py", line 121, in GetThread
'in this thread: %d') % (thread['id'], len(messages))
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'

即使我改变了print ('thread id: %s - 此线程中的消息数:%d') % (thread['id'], len(messages))

to: print ('thread id: %s - 此线程中的消息数: %d') % ('test', 1)

thread id: %s - number of messages in this thread: %d
Traceback (most recent call last):
File "quickstart1.py", line 175, in <module>
main()
File "quickstart1.py", line 151, in main
GetThread(service, EMAIL_LOGIN, thread_id)
File "quickstart1.py", line 120, in GetThread
print ('thread id: %s - number of messages in this thread: %d') % ('test', 1)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'

我仍然得到同样的错误。知道为什么吗?

最佳答案

来自 Google 的这个示例是为 Python 2 编写的,但您在 Python 3 上或通过 from __future__ import print_function 使用它。非常不幸的是,Google 在他们的文档示例中使用了过时版本的 Python,尽管至少从最后的 print 语句和 except Exception, e 可以明显看出,已更改为 except errors.HttpError, error:


具体来说:

print ('thread id: %s - number of messages '
'in this thread: %d') % (thread['id'], len(messages))

是为 Python 2 编写的,应该改为

print(('thread id: %s - number of messages '
'in this thread: %d') % (thread['id'], len(messages)))

在 Python 3 中,print 是一个返回 None 的函数,所以这种语句被解析为

(print('something')) % (something, something)

即运算符 % 应用于 print 和元组 (thread['id'], len(messages)) 的返回值。

但是在 Python 2 中,print 是一个语句,print 关键字之后的所有内容都将首先计算并打印;所以在 Python 2 中,它被解析为

print (('something') % (something, something))

在整个内容周围添加额外的括号可以使其在 Python 3 上正常工作。


如果语句没有那些额外的括号,你会得到 SyntaxError: Missing parentheses in call to 'print'” mean in Python?"这会使错误立即显而易见。

关于python - 格式化程序有问题 : "unsupported operand type(s) for %: ' NoneType' and 'tuple' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36104885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com