简体   繁体   English

如何修复 Python 中嵌套字典中的“TypeError:元组索引必须是整数或切片,而不是 str”错误?

[英]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.

相关问题 如何解决 Python 中的“TypeError:元组索引必须是整数或切片”错误? - How to solve "TypeError: tuple indices must be integers or slices" error in Python? 类型错误:元组索引必须是整数或切片,而不是 str - TypeError: tuple indices must be integers or slices, not str TypeError:元组索引必须是整数或切片,而不是str Python情绪鸣叫 - TypeError: tuple indices must be integers or slices, not str Python sentiment tweet 类型错误:元组索引必须是整数或切片,而不是 str postgres/python - TypeError: tuple indices must be integers or slices, not str postgres/python 我该如何解决此错误? TypeError:列表索引必须是整数或切片,而不是str:discord.py - How do i fix this error? TypeError: list indices must be integers or slices, not str: discord.py 如何修复错误TypeError:列表索引必须是整数或切片,而不是python中的str - how to fix error TypeError: list indices must be integers or slices, not str in python 从嵌套字典中提取信息:TypeError:列表索引必须是整数或切片,而不是 str - pulling information from nested dictionaries: TypeError: list indices must be integers or slices, not str Python 错误:元组索引必须是整数或切片,而不是 str - Python Error: tuple indices must be integers or slices, not str 为什么会出现此错误:TypeError:元组索引必须是整数或切片,而不是str - Why do I get this error: TypeError: tuple indices must be integers or slices, not str 如何修复“TypeError: list indices must be integers or slices, not str.”? - How to fix " TypeError: list indices must be integers or slices, not str. "?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM