自动化-Yaml文件写入函数封装

1.文件布局

打开文件修改读取方式为w
dump函数写入文件
写入中文 使用allow_unicode=True

class ReadConfiYaml:
    def __init__(self,yaml_file):
        self.yaml_file=yaml_file

        def write_yaml(self):
        with open(self.yaml_file, encoding='utf-8',mode='w') as f:
            data={'student': [{'name': 'wx'},{'age': '19'} ]}
            yaml.dump(data,stream=f,allow_unicode=True)



if __name__ == '__main__':
    rc=ReadConfiYaml('config.yaml')
    rc.write_yaml()

3.结果

原文地址:https://www.cnblogs.com/luoditao/p/15068973.html