【问题标题】:Extracting float from JSON file in Python在 Python 中从 JSON 文件中提取浮点数
【发布时间】:2022-01-19 20:44:27
【问题描述】:

背景 -
我有一个返回 API 响应的函数。如果api_response 包含meta,它将打印响应,否则该函数将循环通过一些提取并打印idpercent_complete 密钥对值的代码。目的是 API 响应将返回这些值,以显示数据可供用户单独调用有多接近。

问题 - 当id 被返回并打印时没有问题,percent_complete 返回空白。

功能 -

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 没有,而是一个浮点数。我的代码是否需要进行一些更改才能适应?

【问题讨论】:

  • 你需要找val['attributes']['percent_complete']
  • 感谢您的意见。我尝试使用res = [val['attributes']['percent_complete'] for key, val in api_response.items() if 'percent_complete' in val]' which uses the key values (rather than variables and res = [val['attributes'][percent_value] for key, val in api_response.items() if percent_value in val]`。我们还缺少其他东西吗?
  • 你在最后的比较中也需要它:if percent_value in val['attributes']
  • 你也可以使用 get 和一个默认的空值来简化它:res = [val['attributes'].get(percent_value, '') for key, val in api_response.items()] (this will add empty items to the list, but they will 'disappear' after the join`。
  • 所以我尝试了res = [val['attributes'][percent_value] for key, val in api_response.items() if percent_value in val['attributes']] 并获得了TypeError: list indices must be integers or slices, not str,它指出了我们一直在处理的代码行。为什么会这样?

标签: python while-loop key


【解决方案1】:

我设法实现了如下描述的结果 -

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()

【讨论】:

    猜你喜欢
    • 2021-09-25
    • 1970-01-01
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多