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

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

 更新時(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)論

武义县| 金山区| 巴里| 吉首市| 义马市| 济源市| 古交市| 桐梓县| 望江县| 古浪县| 东至县| 合川市| 黑山县| 梨树县| 阿拉善右旗| 怀宁县| 睢宁县| 桃园县| 读书| 祥云县| 榆树市| 清水河县| 永定县| 襄汾县| 泾阳县| 长乐市| 神池县| 凌云县| 吕梁市| 桐柏县| 临潭县| 营山县| 铜陵市| 芮城县| 汝南县| 志丹县| 凤台县| 平顶山市| 乌鲁木齐市| 沭阳县| 元阳县|