gpt4 book ai didi

python - 尝试从 json 输入构建列表时出现 "string indices must be integers"错误

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:05 27 4
gpt4 key购买 nike

我正在尝试从 Python 3.7 中的 json 文件创建的字典中创建一个列表。

json 文件具有以下结构:

watches 
collection
model
0 {…}
1
rmc
0 "value_I_need"
1 "value_I_need"

json 提取:

{"watches":{"collection":{"event_banner":{"type":"banner","margin":false,"mobile_layer":true,"class":"tdr-banners-events","media":{"type":"image","src":"/public/banners/events/baselworld_2017_navigation.jpg","height":"150px"},"text":{"align":"left","animate":true,"positioning":"left","suptitle":"BANNER_EVENT_A_TITLE","title":"BANNER_EVENT_A_SUPTITLE","title_type":"h2","style":"light","link_text":"BANNER_EVENT_A_LINK_TEXT","link_href":"/magazine/article/baselworld-2017"}},"collection-navigation":{"type":"view","template":"nav.tdr-collection-navigation.tdr-flex.tdr-flex--align-items-center > ul.tdr-collection-navigation__list.tdr-flex.tdr-flex--align-items-flex-start@list","children":[{"type":"view","template":"li.tdr-collection-navigation__item","insert":{"where":"list"},"children":[{"type":"button-gamma","text":"FIND_YOUR_TUDOR_COLLECTION","href":"/search","cssClass":"tdr-button--gamma-collection-navigation","children":[{"type":"new-icon","cssClass":"circleicon dark-reverse-red","insert":{"where":"icon"},"icon":"search","width":"16","height":"16","colorClass":"tdr-icon-dark"}]}]},{"type":"view","template":"li.tdr-collection-navigation__item","insert":{"where":"list"},"children":[{"type":"collection-navigation-item","index":"0","text":"GRID_VIEW_COLLECTION","children":[{"type":"new-icon","cssClass":"red","insert":{"where":"icon"},"icon":"icon-grid","width":"36","height":"36","colorClass":"tdr-icon-dark"}]}]},{"type":"view","template":"li.tdr-collection-navigation__item","insert":{"where":"list"},"children":[{"type":"collection-navigation-item","index":"1","text":"LIST_VIEW_COLLECTION","children":[{"type":"new-icon","cssClass":"red","insert":{"where":"icon"},"icon":"icon-list-3","width":"36","height":"36","colorClass":"tdr-icon-dark"}]}]},{"type":"view","template":"li.tdr-collection-navigation__item.collection-navigation__item--new-collection","insert":{"where":"list"},"children":[{"type":"collection-navigation-item-new-collection","index":"2","text":"FEATURED_SELECTION","children":[{"type":"new-icon","cssClass":"red","insert":{"where":"icon"},"icon":"switch","width":"63","height":"63","colorClass":"tdr-icon-dark"}]}]}]},"collection_filter":{"0":{"route":"all","name":"all_collection","model_page":["black-bay","new-black-bay-fifty-eight","black-bay-32-36-41","new-black-bay-gmt","black-bay-chrono","black-bay-steel","black-bay-s-g","black-bay-dark","black-bay-bronze","north-flag","pelagos","new-1926","style","glamour-double-date","glamour-date-day","glamour-date","heritage-advisor","heritage-chrono","heritage-ranger","fastrider-black-shield","clair-de-rose","classic"]},"1":{"route":"featured-selection","name":"featured_selection","model_page":["glamour-double-date","new-black-bay-32","new-1926","black-bay-chrono"]},"length":2,"all":0,"featured-selection":1},"model":{"0":{"route":"black-bay-32-36-41","watch_model":"black_bay_32_36_41","model_group":"tudor","fam_intro_title":"bb32_36_41_intro_title","fam_intro_text":"bb32_36_41_intro_text","flagship_rmc":"m79580-0003","page_link":"/watches/black-bay-32-36-41/","tags":[],"optional_calibre":false,"no_wrap":true,"family_filter":true,"aggregated":true,"rmc":["m79540-0007","m79540-0009"]},

print(documents)
{'0': {'route': 'black-bay-32-36-41', 'watch_model': 'black_bay_32_36_41', 'model_group': 'tudor', 'fam_intro_title': 'bb32_36_41_intro_title', 'fam_intro_text': 'bb32_36_41_intro_text', 'flagship_rmc': 'm79580-0003', 'page_link': '/watches/black-bay-32-36-41/', 'tags': [], 'optional_calibre': False, 'no_wrap': True, 'family_filter': True, 'aggregated': True, 'rmc': ['m79580-0003', 'm79580-0004',

我的构建列表的代码:

with open('test.json', 'r') as f:
dictionary = json.load(f)
documents = dictionary["watches"]["collection"]["model"]
for document in documents:
models = document["rmc"]
try:
for model in models:
start_urls.append('https://www.example.com/'+document['page_link']+'/'+model+'.html')
except Exception:
pass

回溯错误:

models = document["rmc"]
TypeError: string indices must be integers

rmc 值是模型列表中的另一个列表。因此每个模型可能有另一个 rmc 值列表。

我的目标是创建所有模型的列表,包括它们的变体 (rmc)。

为什么 pyhton 告诉我它是一个字符串,而我相信 rmc 行是以整数列出的?

最佳答案

您似乎认为您的 model 值是一个列表。 JSON 另有说明:

"model":{"0":{"route":"black-bay-32-36-41",

这是一个字典,其键是字符串。您迭代该字典:

for document in documents:

当您以这种方式迭代字典时,您会迭代该字典的,因此document包含字符串“0” 。该字符串不能被另一个字符串作为 document['rmc'] 索引,因此 Python 正确地提示。

您可以通过多种方式修复它。首先,您可以更改阅读模型的方式:

for document in documents:
models = documents[document]['rmc']
...

或者您可以更改迭代字典的方式:

for idx, document in documents.items():
models = document['rmc']

漂亮地打印 JSON,而不是将其保留为一行难以理解的行,可能会更快地提醒您注意此问题。

关于python - 尝试从 json 输入构建列表时出现 "string indices must be integers"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54081264/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com