Python yaml配置檔案讀取

2021-10-10 04:04:14 字數 1628 閱讀 6831

pip install pyyaml

定義區域名:

excel:

選項名: 選項值

如果選項值為數字,那麼會自動轉化為數字型別(int、float)

如果選項值為true、on、true,那麼會自動轉化為true

如果選項值為false、off、false,那麼會自動轉化為false

其他有字母的,讀取出來都為字串型別

filename: 「testcase.txt」

real_result_column: 7

result_column: 8

api:

api_version: lemonban.v2

api_version:

base_url: 『

import yaml

import os

class

handyaml

:def

__init__

(self,filename=

none):

# 獲取當前檔案的上一級目錄

path_dir = os.path.abspath(os.path.join(os.path.dirname(

"__file__"

), os.path.pardir)

)if filename is

none

: self.filename =path_dir+

"\\confs\\testcases.yaml"

else

: self.filename = filename

with

open

(self.filename,encoding=

"utf-8")as

file

:# full_load將yml檔案中的資料轉為python的資料型別(load棄用)

self.config_data = yaml.full_load(

file

)def

get_data

(self,section,option)

:return self.config_data[section]

[option]

do_yaml = handyaml(

)if __name__ ==

"__main__"

:#filenames1="testcases.yaml"

do_yaml = handyaml(

) data=do_yaml.get_data(

"excel"

,"filename"

)print

(data)

print

(do_yaml.get_data(

"api"

,"base_url"

))

yaml_w =

}with

open

("testcases.yaml"

,"w"

)as f:

# yaml.dump 將python資料轉化成yaml格式

yaml.dump(yaml_w,f)

讀配置檔案

tchar szpath max path memset szpath,0,sizeof szpath getmodulefilename null,szpath,max path cstring strpath szpath strpath strpath.left strpath.reverse...

讀配置檔案2

資料 host 1 ip 16.173.234.228 port 21,80,873,3306 cpu 10 freemem 3 conconn 1000 checkgw 1 freedisk 10 swapused 30000 host host 2 ip 16.173.235.25 port 2...

Php讀配置檔案

馬永佔,myz,mayongzhan php讀配置檔案 馬永佔,myz,mayongzhan 要讀的格式 php.ini this is demo by 馬永佔 myz global i 1j 2 k 3 self a 11 b 22 c 33 讀的方法 name test4.php date mo...