从 Python 中的 JSON 文件中提取浮点数
Extracting float from JSON file in Python
背景 -
我有一个 return 是 API 响应的函数。如果 api_response
包含 meta
它将打印响应,否则该函数循环通过一些提取和打印 id
和 percent_complete
密钥对值的代码。目的是 API 响应将 return 这些值,以显示用户可以单独调用数据有多接近。
问题 - 虽然 id
已 return 编辑和打印没有问题,但 percent_complete
return 为空白。
函数-
def unpack_response():
api_response = api_call()
# Code Block # 1
while "meta" not in api_response:
id_value = "id"
res = [val[id_value] for key, val in api_response.items() if id_value in val]
id_value = "".join(res)
percent_value = "percent_complete"
res = [val[percent_value] for key, val in api_response.items() if percent_value in val]
percent_value = "".join(res)
print(f' Your data requested, associated with ID: {id_value} is {percent_value} complete!')
time.sleep(5)
continue
# Code Block # 2
if "meta" in api_response:
print(api_response)
示例打印输出 - 显示每次循环迭代当前打印内容的示例:
Your data requested, associated with ID: 2205686 is complete!
API 响应 - 我成功提取 id
密钥对值的响应示例,但是 percent_complete
密钥对值为空:
{'data': {'id': '2205686',
'type': 'jobs',
'attributes': {'job_type': 'PORTFOLIO_VIEW_RESULTS',
'started_at': '2021-12-16T18:59:50Z',
'parameters': {'end_date': '2021-12-14',
'output_type': 'json',
'view_id': 304078,
'portfolio_id': 1,
'portfolio_type': 'firm',
'start_date': '2021-12-14'},
'percent_complete': 0.19,
'status': 'In Progress'},
'relationships': {'creator': {'links': {'self': '/v1/jobs/2205679/relationships/creator',
'related': '/v1/jobs/2205679/creator'},
'data': {'type': 'users', 'id': '731221'}}},
'links': {'self': '/v1/jobs/2205679'}},
'included': []}
我的想法 - 与 id
(在引号中有一个密钥对值)不同,percent_complete
没有并且是一个浮点数。我的代码是否需要一些更改才能适应?
我设法实现了如下描述的结果-
def unpack_response():
api_response = api_call()
# Code Block # 1
while "meta" not in api_response:
id_value = "id"
res1 = [val[id_value] for key, val in api_response.items() if id_value in val]
id_value = "".join(res1)
percent_value = "percent_complete"
# res2 = [val['attributes'][percent_value] for key, val in api_response.items() if percent_value in val['attributes']]
res2 = [val['attributes'].get(percent_value, '') for key, val in api_response.items()]
percent_value = "".join(res2)
print(f' Your data requested, associated with ID: {id_value} is {percent_value} complete!')
time.sleep(5)
continue
# Code Block # 2
if "meta" in api_response:
print(api_response)
unpack_response()
背景 -
我有一个 return 是 API 响应的函数。如果 api_response
包含 meta
它将打印响应,否则该函数循环通过一些提取和打印 id
和 percent_complete
密钥对值的代码。目的是 API 响应将 return 这些值,以显示用户可以单独调用数据有多接近。
问题 - 虽然 id
已 return 编辑和打印没有问题,但 percent_complete
return 为空白。
函数-
def unpack_response():
api_response = api_call()
# Code Block # 1
while "meta" not in api_response:
id_value = "id"
res = [val[id_value] for key, val in api_response.items() if id_value in val]
id_value = "".join(res)
percent_value = "percent_complete"
res = [val[percent_value] for key, val in api_response.items() if percent_value in val]
percent_value = "".join(res)
print(f' Your data requested, associated with ID: {id_value} is {percent_value} complete!')
time.sleep(5)
continue
# Code Block # 2
if "meta" in api_response:
print(api_response)
示例打印输出 - 显示每次循环迭代当前打印内容的示例:
Your data requested, associated with ID: 2205686 is complete!
API 响应 - 我成功提取 id
密钥对值的响应示例,但是 percent_complete
密钥对值为空:
{'data': {'id': '2205686',
'type': 'jobs',
'attributes': {'job_type': 'PORTFOLIO_VIEW_RESULTS',
'started_at': '2021-12-16T18:59:50Z',
'parameters': {'end_date': '2021-12-14',
'output_type': 'json',
'view_id': 304078,
'portfolio_id': 1,
'portfolio_type': 'firm',
'start_date': '2021-12-14'},
'percent_complete': 0.19,
'status': 'In Progress'},
'relationships': {'creator': {'links': {'self': '/v1/jobs/2205679/relationships/creator',
'related': '/v1/jobs/2205679/creator'},
'data': {'type': 'users', 'id': '731221'}}},
'links': {'self': '/v1/jobs/2205679'}},
'included': []}
我的想法 - 与 id
(在引号中有一个密钥对值)不同,percent_complete
没有并且是一个浮点数。我的代码是否需要一些更改才能适应?
我设法实现了如下描述的结果-
def unpack_response():
api_response = api_call()
# Code Block # 1
while "meta" not in api_response:
id_value = "id"
res1 = [val[id_value] for key, val in api_response.items() if id_value in val]
id_value = "".join(res1)
percent_value = "percent_complete"
# res2 = [val['attributes'][percent_value] for key, val in api_response.items() if percent_value in val['attributes']]
res2 = [val['attributes'].get(percent_value, '') for key, val in api_response.items()]
percent_value = "".join(res2)
print(f' Your data requested, associated with ID: {id_value} is {percent_value} complete!')
time.sleep(5)
continue
# Code Block # 2
if "meta" in api_response:
print(api_response)
unpack_response()