[Python] List object has no attribute 'items' after parsing JSON file

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

    I wanted to enumerate dictionary items that are in a json file.

    offers.json

    {
    "offer": [
    {"bleach": 14 ,
    "tshirt": 25,
    "notebook": 6,
    "mayo": 3,
    "kiwi": 11,
    "perfume": 68,
    "hat": 2,
    "toy": 17}
    ]
    }


    python file

    import json
    with open("offers.json",'r')as f:
    offer=json.load(f)['offer']

    for i, (key, value) in enumerate(offer.items()):
    print(f"{str(i + 1)} {key} {value:02} $")


    output

    AttributeError: 'list' object has no attribute 'items'

    Continue reading...

Compartilhe esta Página