python写入yaml

python写json数据到文件,f.write(json.dumps(jsondata,indent=4))

indent格式化的json

终于整出来了,私藏。

 

import json
from ruamel import yaml

class  Dag:
  def __init__(self):
    self.case_suite="xy_datasource_sink_tes"
    self.descrpiton="新氧test"
    #赋值test.json文件到yaml,定义字典dic2
    with open('test.json','r',encoding='utf-8') as f_load:
        dic2=json.load(f_load)
    dic1=[{
        'moduel_class':'TestDatasource',
        'desc':'从ES到HDFS业务场景',
        'cases':[{
        'story':'test_xinyang',
        'uri':'/datasource',
        'method':'post',
        'type':'json',
        'headers':
            {
                'Content-Type': 'application/json',
                'Token': '@super_token'},
        "data":dic2,
        "set_up": "",
        "tear_down": "",
        "asserts": "200"
        }],
        }
    ]
    self.moduels=dic1

if __name__ == '__main__':
    dog = Dag()
    file = 'demo1.yaml'
    stream = open(file, 'w',encoding='utf-8')
    #生成 yaml 文件
    # yaml.safe_dump(dog.__dict__, stream=stream,default_flow_style=False)
    yaml.dump(dog.__dict__, stream=stream, Dumper=yaml.RoundTripDumper, allow_unicode=True)

 

你可能感兴趣的:(开发)