【问题标题】:Trying to print every nth element in strange data format that is identified as list [duplicate]尝试以标识为列表的奇怪数据格式打印每个第 n 个元素 [重复]
【发布时间】:2019-05-17 08:58:30
【问题描述】:

我通过一个被 python 识别为列表的 API 提取数据,但看起来不像一个。

这是返回数据的示例:

[{'product_id': '41111', 'type': 'configurable', 'set': '4', 'sku': 'CM5555-CONF', 'position': '1'}, {'product_id': '42222', 'type': 'configurable', 'set': '4', 'sku': 'CM6666-CONF', 'position': '1'}

这是一个名为 temp 的变量。

当我执行 print(type(temp)) 时,返回 <class 'list'>

我正在尝试访问所有产品 ID 的列表,以便将它们提供给 API 的不同请求。

当我执行print(temp[::1]) 时,会返回整个列表(或任何列表),就好像我只是单独执行了print(temp)

如何自己打印 product_id 列表?

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    您是正确的,它是一个列表,但列表内是 {},它们是字典。你必须参考他们的钥匙才能把他们拉出来。试试:

    for datadict in temp:
        print(datadict['product_id'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-09
      • 1970-01-01
      • 2018-03-11
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      相关资源
      最近更新 更多