[Python] Python how to set dict key as 01, 02, 03 ... when using dpath?

Discussão em 'Python' iniciado por Stack, Novembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    Let's say we want to create/maintain a dict with below key structure

    {
    "a": {"bb": {"01": "some value 01",
    "02": "some value 02",
    } },
    }


    We use dpath .new() as below to do this

    import dpath

    d=dict() ; print(d) # d={}
    dpath.new(d,'a/bb/00/c', '00val') ; print(d) # d={'a': {'bb': [{'c': '00val'}]}}
    # d # NOTE this will set :bb as a list, and set at bb[00] ie idx=0 of list bb
    # d # what we want instead is d={'a': {'bb': {'00': {'c': '00val'} }}}
    # d # what we want is NOT d={'a': {'bb': [ {'c': '00val'} ]}}


    So when using 00 in the path, dpath translate it into list index at 0 instead of dict key 00

    The question is how to set key as 00? Currently I have to dodge it by setting prefix s ie s00 s01 s02

    Continue reading...

Compartilhe esta Página