最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python參數(shù)解析器configparser簡(jiǎn)介

 更新時(shí)間:2022年12月21日 09:06:35   作者:味卜鮮碼  
configparser是python自帶的配置參數(shù)解析器,可以用于解析.config文件中的配置參數(shù),ini文件中由sections(節(jié)點(diǎn))-key-value組成,這篇文章主要介紹了Python參數(shù)解析器configparser,需要的朋友可以參考下

1.configparser介紹

configparser是python自帶的配置參數(shù)解析器??梢杂糜诮馕?config文件中的配置參數(shù)。ini文件中由sections(節(jié)點(diǎn))-key-value組成

2.安裝:

pip install configparse

3.獲取所有的section

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#讀取config,有中文注意編碼
#獲取所有的section
sections = cf.sections()
print(sections)
#輸出:['CASE', 'USER']

4.獲取指定section下的option

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#讀取config,有中文注意編碼
#獲取指定section下所有的option
options = cf.options("CASE")
print(options)
#輸出:['caseid', 'casetitle', 'casemethod', 'caseexpect']

5.獲取指定section的K-V

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#讀取config,有中文注意編碼#獲取指定section下的option和value,每一個(gè)option作為一個(gè)元祖[(),(),()]
alls = cf.items("CASE")
print(alls)
#輸出:[('caseid', '[1,2,3]'), ('casetitle', '["正確登陸","密碼錯(cuò)誤"]'), ('casemethod', '["get","post","put"]'), ('caseexpect', '0000')]

6.獲取指定value(1)

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#讀取config,有中文注意編碼
#獲取指定section下指定option的value
caseid = cf.get("CASE","caseid")
print(caseid)

7.獲取指定value(2)

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#讀取config,有中文注意編碼
#獲取指定section下指定option的value
caseid = cf["CASE"]["caseid"]
print(caseid)
#輸出:[1,2,3]

8.value數(shù)據(jù)類型

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#讀取config,有中文注意編碼
#value數(shù)據(jù)類型
user = cf["USER"]["user"]
print(type(user))
#輸出:<class 'str'>

9.value數(shù)據(jù)類型還原eval()

import configparser

cf = configparser.ConfigParser()
cf.read("case.config",encoding="utf8")#讀取config,有中文注意編碼
#value數(shù)據(jù)類型還原
user = cf["USER"]["user"]
print(type(user))#輸出:<class 'str'>
user = eval(user)
print(type(user))#輸出:<class 'list'>

10.封裝

import configparser

class GetConfig():
    def get_config_data(self,file,section,option):
        cf = configparser.ConfigParser()
        cf.read(file, encoding="utf8")  # 讀取config,有中文注意編碼
        # 返回value
        return cf[section][option]

if __name__ == '__main__':
    values = GetConfig().get_config_data("case.config","USER","user")
    print(values)
    #輸出:[{"username":"張三","password":"123456"},{"username":"李四"}]

到此這篇關(guān)于Python參數(shù)解析器configparser的文章就介紹到這了,更多相關(guān)Python參數(shù)解析器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

莒南县| 阿瓦提县| 玛沁县| 白银市| 孝感市| 田林县| 华安县| 曲阜市| 房产| 岗巴县| 广东省| 南丹县| 长武县| 河北区| 五大连池市| 新蔡县| 富宁县| 庄浪县| 凉山| 台中县| 科尔| 鹿泉市| 广宁县| 保山市| 临泽县| 扶风县| 沙田区| 铜梁县| 文山县| 襄垣县| 岢岚县| 高台县| 徐汇区| 兴安县| 新沂市| 治多县| 禹州市| 华池县| 读书| 乳山市| 夏河县|