简而言之,对于数据科学类(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))
这是打印代码。仅适用于项目 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/