zoukankan      html  css  js  c++  java
  • python yaml文件数据按原有的数据顺序dump

    yml文件的更新后工具类:

    import os
    import yaml
    
    
    class YamlUtils():
        def __init__(self,folder_name='config'):
            self.foler_name = folder_name
    
        def get_yaml_load(self, file_name):
            yaml_filename = self.__get_yaml_file(file_name)
            f = open(yaml_filename, encoding='utf-8')
            yaml_load = yaml.full_load(f)
            return yaml_loaddef __get_yaml_file(self, file_name):
            """
            :param file_name: the filename of the configfile
            :return: the objection of the config
            """
            try:
                yaml_file = self.__get_file_path(self.foler_name, file_name)
                return yaml_file
            except Exception as e:
                print("read config file error:" + str(e))
    
        @staticmethod
        def __get_file_path(folder_name, file_name):
            """
            :param folder_name: the directory of the config ,the default directory is config
            :param file_name: the filename of the configfile
            :return: the objection of the config
            """
            try:
                config_path = os.path.abspath('..')
                folder_path = os.path.join(config_path, folder_name)
                file_path = os.path.join(folder_path, file_name)
                return file_path
            except Exception as e:
                print('read config file failed ' + str(e))
        def set_yaml(self, file_name,content):
            """
            调用此方法时,需新建一个YamlUtils().set_yaml(file_name,content)
            用原来的设置不会生效
            default_flow_style=False : 表示dump后的字典数据全部以yml格式显示,默认为为True
            sort_keys=False : 表示dump后的字典数据按原有的顺序示,为True时按字母的排序展示,默认为为True
            """
            yaml_filename = self.__get_yaml_file(file_name)
            f = open(yaml_filename,'w', encoding='utf-8')
            yaml_dump = yaml.dump(content,f,allow_unicode=True, default_flow_style=False,sort_keys=False)
            # print("数据更新完成")
            return yaml_dump

    yml文件原有数据顺序:

    更改字段值后的顺序:

     出现原因是:

    yaml.dump(content,f,allow_unicode=True, default_flow_style=False,sort_keys=True)
    该行的代码中
    sort_keys的默认值为True,改为Flase后可修复代顺序变化的问题
  • 相关阅读:
    appium python api(转)
    make clean 与 make distclean 的区别
    实参相依查找[条款25]----《C++必知必会》
    成员函数查找[条款24]---《C++必知必会》
    C++匿名名字空间
    程序界面多语言切换功能如何实现
    C 语言中 define 的全部使用方法总结
    #if defined(__cplusplus)
    伯乐在线
    jsp手动分页
  • 原文地址:https://www.cnblogs.com/pandaly/p/13274150.html
Copyright © 2011-2022 走看看