[英]How do you fix "TypeError: tuple indices must be integers or slices, not str" error from nested dictionaries in Python?
I am having a TypeError "Exception has occurred: TypeError tuple indices must be integers or slices, not str".我有一个 TypeError “发生异常:TypeError 元组索引必须是整数或切片,而不是 str”。 I am using python 3. Here is my code below:
我正在使用 python 3。下面是我的代码:
with open('item_info.json') as item_info:
item_data = json.load(item_info)
for collection in item_data:
print(f'\n {collection}')
for skin in item_data[collection].items():
if skin['rarity'] == 'Consumer Grade':
print(skin)
I have a large JSON file that I have imported using the JSON library in the variable item_data
, collection
is a dictionary containing nested dictionaries once loaded by the JSON library and skin
is every individual dictionary within the collection
, holding a key named 'rarity'
. I have a large JSON file that I have imported using the JSON library in the variable
item_data
, collection
is a dictionary containing nested dictionaries once loaded by the JSON library and skin
is every individual dictionary within the collection
, holding a key named 'rarity'
. I would like to print every skin
that has a value of 'rarity'
equal to 'Consumer Grade'
.我想打印每一个
'rarity'
值等于'Consumer Grade'
skin
。 I am trying to do this by nested for loops, as there are multiple collections and I'd like to loop through every skin in every collection as a check.我试图通过嵌套的 for 循环来做到这一点,因为有多个 collections 并且我想遍历每个集合中的每个皮肤作为检查。
Here is a sample of the imported JSON held in item_data
:这是 item_data 中保存的导入
item_data
的示例:
{
"2018_inferno": {
"MP5-SD | Dirt Drop (Factory New)": {
"rarity": "Consumer Grade"
},
"MAC-10 | Calf Skin (Factory New)": {
"rarity": "Industrial Grade"
}
},
"2018_nuke": {
"Five-SeveN | Coolant (Factory New)": {
"rarity": "Consumer Grade"
},
"Negev | Bulkhead (Factory New)": {
"rarity": "Industrial Grade"
}
},
"alpha": {
"Five-SeveN | Anodized Gunmetal (Factory New)": {
"rarity": "Consumer Grade"
},
"P250 | Facets (Field-Tested)": {
"rarity": "Industrial Grade"
}
}
}
The output should look something like this: output 应如下所示:
2018_inferno
{"MP5-SD | Dirt Drop (Factory New)": {"rarity": "Consumer Grade"}}
2018_nuke
{"Five-SeveN | Coolant (Factory New)": {"rarity": "Consumer Grade"}}
alpha
{"Five-SeveN | Anodized Gunmetal (Factory New)": {"rarity": "Consumer Grade"}}
or (more desirable)或(更可取)
2018_inferno
"MP5-SD | Dirt Drop (Factory New)"
2018_nuke
"Five-SeveN | Coolant (Factory New)"
alpha
"Five-SeveN | Anodized Gunmetal (Factory New)"
skin
is a tuple of the the key-value pairs from item_data[collection].items()
. skin
是来自item_data[collection].items()
的键值对的元组。 Tuples cannot be accessed using string indicies ( skin['rarity']
)无法使用字符串索引(
skin['rarity']
)访问元组
You can iterate both keys and values at the same time, to make it easier to iterate, for example您可以同时迭代键和值,以便更容易迭代,例如
for collection, item in item_data.items():
print(f'\n{collection}')
for k, v in item.items():
if v.get('rarity') == 'Consumer Grade':
print(k)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.