【发布时间】:2021-01-07 22:02:21
【问题描述】:
免责声明:我对 python 很陌生,不知道自己在做什么,我是在网上自学。
我有一些看起来像这样的代码
代码:
from request import get # Ed: added for clarity
myurl = URLBASE + args.key
response = get(myurl)
# check key is valid
json = response.text # Ed: this is a requests.Response object
print(json)
if json is None:
sys.exit("Problem getting API data, check your key")
print("how did i get here")
输出:
null
how did i get here
但我不知道这是怎么可能的......它在打印中字面上说它是空的,但在'if'中不匹配。任何帮助将不胜感激。
谢谢
【问题讨论】:
-
你从未解析过 JSON,所以它是字符串
"null"。不确定get是什么,但请尝试json = response.json。 -
字符串
"null"不是对象None。 -
唯一的
None是None本身。 -
@Ry- 有正确的想法,但您需要使用
response.json(),因为response.json只会让您陷入类似的陷阱。
标签: python if-statement nonetype