如何在python中處理配置文件代碼實(shí)例
配置文件是一種計(jì)算機(jī)文件,可以為一些計(jì)算機(jī)程序配置參數(shù)和初始設(shè)置,在內(nèi)容形式上是一個(gè)一個(gè)鍵值對的記錄。
testcase.yaml文件:
excel:filename: 'testcase.xlsx'
將yaml庫做二次封裝:
import yamlclass HandleYaml: def __init__(self, filename=None): if filename is None: self.filename = ’testcase.yaml’ else: self.filename = filename with open(filename, encoding='utf-8') as file: # 用上下文管理器打開yaml配置文件 self.data = yaml.full_load(file) # 加載yaml文件,返回一個(gè)嵌套字典的字典 def get_data(self, section, option): return self.data[section][option]if __name__ == '__main__': s = HandleYaml() s.get_data(’excel’, ’filename’)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python web框架的總結(jié)2. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式3. Python如何進(jìn)行時(shí)間處理4. python使用ctypes庫調(diào)用DLL動態(tài)鏈接庫5. 詳解Python模塊化編程與裝飾器6. Python基于pyjnius庫實(shí)現(xiàn)訪問java類7. Python使用shutil模塊實(shí)現(xiàn)文件拷貝8. Python實(shí)現(xiàn)迪杰斯特拉算法過程解析9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. python裝飾器三種裝飾模式的簡單分析
