How can I parse a YAML file in Python

后端 未结 8 1184
醉话见心
醉话见心 2020-11-22 14:54

How can I parse a YAML file in Python?

8条回答
  •  没有蜡笔的小新
    2020-11-22 15:18

    I use ruamel.yaml. Details & debate here.

    from ruamel import yaml
    
    with open(filename, 'r') as fp:
        read_data = yaml.load(fp)
    

    Usage of ruamel.yaml is compatible (with some simple solvable problems) with old usages of PyYAML and as it is stated in link I provided, use

    from ruamel import yaml
    

    instead of

    import yaml
    

    and it will fix most of your problems.

    EDIT: PyYAML is not dead as it turns out, it's just maintained in a different place.

提交回复
热议问题