python 的 yaml 操作例子

依赖


rpm -qa | grep PyYAML
PyYAML-3.10-3.1.el6.x86_64

例子代码


#!/usr/bin/python
import os, sys,yaml

fname="index.yaml"

f = open(fname,"r")
x=yaml.load(f)

#print(x)

dist={}

dlist="centos6.x86_64|centos7.x86_64"
arr = dlist.split("|")

for ite in arr:
dist[ite] = {}
dist[ite]['release'] = "{{.gitcount}}.{{.dist}}"
x['distribution'] = dist
#print "\n\n"
#print x
#print "\n"
ret = yaml.dump(x)
print ret

This entry was posted in 运维相关. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Both comments and trackbacks are currently closed.

2 Comments

  1. cme
    Posted 2019年1月22日 at 下午2:40 | Permalink

    ## 正则表达式匹配合法名字: 由字母数字和下划线和减号组成的字符串,并且开头和结尾只能是字母或者数字。


    import re
    patt="^[a-zA-Z][a-zA-Z0-9_\-]*[a-zA-Z0-9]$"
    >>> print re.match(patt, "-fdfd32_3-2-")
    None
    >>> print re.match(patt, "-fdfd32_3-2")
    None
    >>> print re.match(patt, "fdfd32_3-2")
    <_sre .SRE_Match object at 0x7f07f70acb90>

  2. cme
    Posted 2019年1月23日 at 下午4:15 | Permalink

    修改默认格式:

    ret = yaml.dump(x, default_flow_style=False)