【发布时间】:2018-11-28 12:45:53
【问题描述】:
我有一本包含多个 try-except 语句的字典。 我试图用 r.get() 检索值,但它给我抛出了一个带有 NoneObject 的类型错误。我知道 .get() 的默认参数是 None,但它不起作用。我有多个列表,我在每次迭代时附加来自不同字典值的数据。
如何将代码简化为单个 try-except 语句?谢谢!
这是我的代码:
for num in issue_number:
print(num)
Response = requests.get(f'https://example.com/rest/api/2/issue/Proj-{num}?expand=changelog&maxResults =1000', auth=(example))
r = Response.json()
try:
task_list.append(r['key'])
except TypeError:
task_list.append('NA')
try:
summary_list.append(r['fields']['summary'])
except TypeError:
summary_list.append('NA')
try:
assignee_list.append(r['fields']['assignee']['displayName'])
except TypeError:
assignee_list.append('NA')
try:
created_list.append(r['fields']['created'])
except:
created_list.append('NA')
try:
status_list.append(r['fields']['status']['name'])
except:
status_list.append('NA')
try:
due_date_list.append(r['fields']['duedate'])
except:
due_date_list.append('NA')
try:
resolution_list.append(r['fields']['resolution']['name'])
except:
resolution_list.append('NA')
try:
resolution_date_list.append(r['fields']['resolutiondate'])
except:
resolution_date_list.append('NA')
【问题讨论】:
-
你的回复是
rNone吗? -
你好@doctorlove!不,对 r 的响应是一个包含 5 个字典的大字典。
标签: python request try-except