python?yaml文件數(shù)據(jù)按原有的數(shù)據(jù)順序dump問(wèn)題小結(jié)
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_load def __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): """ 調(diào)用此方法時(shí),需新建一個(gè)YamlUtils().set_yaml(file_name,content) 用原來(lái)的設(shè)置不會(huì)生效 default_flow_style=False : 表示dump后的字典數(shù)據(jù)全部以yml格式顯示,默認(rèn)為為True sort_keys=False : 表示dump后的字典數(shù)據(jù)按原有的順序示,為True時(shí)按字母的排序展示,默認(rèn)為為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("數(shù)據(jù)更新完成") return yaml_dump
yml文件原有數(shù)據(jù)順序:
更改字段值后的順序:
出現(xiàn)原因是:
yaml.dump(content,f,allow_unicode=True, default_flow_style=False,sort_keys=True) 該行的代碼中sort_keys的默認(rèn)值為True,改為Flase后可修復(fù)代順序變化的問(wèn)題
到此這篇關(guān)于python yaml文件數(shù)據(jù)按原有的數(shù)據(jù)順序dump的文章就介紹到這了,更多相關(guān)python yaml文件dump內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python連接SQLite數(shù)據(jù)庫(kù)操作實(shí)戰(zhàn)指南從入門到精通
在Python中使用SQLite進(jìn)行數(shù)據(jù)庫(kù)操作時(shí),我們將深入研究SQLite數(shù)據(jù)庫(kù)的創(chuàng)建、表格管理、數(shù)據(jù)插入、查詢、更新和刪除等關(guān)鍵主題,幫助你全面了解如何使用SQLite進(jìn)行數(shù)據(jù)庫(kù)操作2023-11-11Python程序員開(kāi)發(fā)中常犯的10個(gè)錯(cuò)誤
這篇文章主要介紹了Python程序員開(kāi)發(fā)中常犯的10個(gè)錯(cuò)誤,不知道你有沒(méi)有中槍呢,需要的朋友可以參考下2014-07-07在python plt圖表中文字大小調(diào)節(jié)的方法
今天小編就為大家分享一篇在python plt圖表中文字大小調(diào)節(jié)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07解決pyinstaller打包exe可執(zhí)行文件后運(yùn)行找不到pandas或者XXX模塊
這篇文章主要介紹了解決pyinstaller打包exe可執(zhí)行文件后運(yùn)行找不到pandas或者XXX模塊問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11pandas實(shí)現(xiàn)處理TB級(jí)別的數(shù)據(jù)
這篇文章主要介紹了pandas實(shí)現(xiàn)處理TB級(jí)別的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04Pyhton爬蟲(chóng)知識(shí)之正則表達(dá)式詳解
正則表達(dá)式又稱規(guī)則表達(dá)式,計(jì)算機(jī)科學(xué)的一個(gè)概念,正則表達(dá)式通常被用來(lái)檢索、替換那些符合某個(gè)模式(規(guī)則)的文本,這篇文章主要給大家介紹了關(guān)于Pyhton爬蟲(chóng)知識(shí)之正則表達(dá)式的相關(guān)資料,需要的朋友可以參考下2022-04-04YOLOv5構(gòu)建安全帽檢測(cè)和識(shí)別系統(tǒng)使用詳解
這篇文章主要為大家介紹了YOLOv5構(gòu)建安全帽檢測(cè)和識(shí)別系統(tǒng)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04python中csv文件數(shù)據(jù)顏色設(shè)置方式
這篇文章主要介紹了python中csv文件數(shù)據(jù)顏色設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python 批量添加的button 使用同一點(diǎn)擊事件的方法
今天小編就為大家分享一篇python 批量添加的button 使用同一點(diǎn)擊事件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07