python - 正在附加 JSON 文件 : "TypeError: list indices must be integers or slices, not str"

标签 python json python-3.x walmart-api

简而言之,对于数据科学类(class),我在 Walmart 上运行循环 (Python 3) 来查询商品信息并构建以 JSON 保存的综合数据集。 (在本例中,笔记本电脑属于沃尔玛 API 中的电子产品类别。)

问题: --我可以阅读第一组 25 条内容 --当我追加第 26 项以上时,会产生类型错误

这是我的附加代码

    a = []
    if not os.path.isfile('filename.json'):
        a.append(entry['items'])
        with open('filename.json', mode='w') as f:
            f.write(json.dumps(entry['items'], indent=4))
    else:
        with open('filename.json') as feedsjson:
            feeds = json.load(feedsjson)

        feeds.append(entry['items'])
        with open('filename.json', mode='w') as f:
            f.write(json.dumps(feeds, indent=4))

Here is the json file

这是打印代码。仅适用于项目 1-25

filepath = os.path.join('filename.json')   
with open(filepath) as jsonfile:
    json_data = json.load(jsonfile)

    for i in range(len(json_data)):
        itemId = json_data[i]['itemId']
        print(itemId)

最佳答案

在您的 json 数据中,您有第 26 项的列表。因此您无法在列表中查找键。

换句话说,itemId 不是列表的索引。

关于python - 正在附加 JSON 文件 : "TypeError: list indices must be integers or slices, not str",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51661377/

相关文章:

Python:Webdriver 无法与 css_selector 一起使用

javascript - 如何将数据转换为字符串

oracle - Pandas 和 SQL 炼金术 : Specify Column Data Types

python - 将 Hessian 矩阵定义为零

javascript - Angular JS 和 Symfony2

python - 在 SQLAlchemy 中定义表时,如何将函数(依赖于其他列的表达式)定义为列的默认值?

python - 使用来自 gridsearchcv 的最佳参数

Python输出: "Not Found"

python - Keras:如何创建自定义 Noisy Relu 函数?

javascript - 使用 d3.js v.4 解析日期