欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 读写yaml pythonreadyaml 

程序员文章站 2024-01-08 16:15:04
...
http://mikkel.elmholdt.dk/?p=4

1. install pyyaml:  (http://pyyaml.org/wiki/PyYAMLDocumentation)
1.1 wget http://pyyaml.org/download/pyyaml/PyYAML-3.01.tar.gz
1.2 tar zxvf PyYAML-3.01.tar.gz
1.3 cd PyYAML-3.01 && python setup.py install

# tree format
treeroot:
    branch1:
        name: Node 1
        branch1-1:
            name: Node 1-1
    branch2:
        name: Node 2
        branch2-1:
            name: Node 2-1


import yaml
f = open('tree.yaml')
dataMap = yaml.load(f)
f.close()

# dataMap: 
{'treeroot': {'branch1': {'branch1-1': {'name': 'Node 1-1'},
    'name': 'Node 1'},
    'branch2': {'branch2-1': {'name': 'Node 2-1'},
    'name': 'Node 2'}}}



f = open('newtree.yaml', "w")
yaml.dump(dataMap, f)
f.close()
相关标签: python read yaml

上一篇:

下一篇: