[Python] Control ordering of items called by yaml.load()

Discussão em 'Python' iniciado por Stack, Junho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have a YAML setting file which creates some records in DB:

    setting1:
    name: [item,item]
    name1: text
    anothersetting2:
    name: [item,item]
    sub_setting:
    name :[item,item]


    When I update this file with setting3 and regenerate records in DB by:

    import yaml
    fh = open('setting.txt', 'r')
    setting_list = yaml.load(fh)
    for i in setting_list:
    add_to_db


    it's vital that the order of the settings (ID numbers in DB) stay the same each time, as I'm adding them to the db, and setting3 just gets appended to the yaml.load()'s end so that its ID doesn't confuse any records which are already in the DB.

    At the moment each time I add another setting and call yaml.load() records get loaded in different order which results in different IDs.

    Continue reading...

Compartilhe esta Página