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

1.configparser介紹
configparser是python自帶的配置參數(shù)解析器??梢杂糜诮馕?config文件中的配置參數(shù)。ini文件中由sections(節(jié)點)-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,每一個option作為一個元祖[(),(),()]
alls = cf.items("CASE")
print(alls)
#輸出:[('caseid', '[1,2,3]'), ('casetitle', '["正確登陸","密碼錯誤"]'), ('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":"李四"}]
到此這篇關于Python參數(shù)解析器configparser的文章就介紹到這了,更多相關Python參數(shù)解析器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python的Flask框架中實現(xiàn)登錄用戶的個人資料和頭像的教程
這篇文章主要介紹了Python的Flask框架中實現(xiàn)登錄用戶的個人資料和頭像的教程,這也是各個web框架的最基本功能之一,需要的朋友可以參考下2015-04-04
django的分頁器Paginator 從django中導入類
這篇文章主要介紹了django的分頁器Paginator 從django中導入類,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07
對numpy的array和python中自帶的list之間相互轉化詳解
下面小編就為大家分享一篇對numpy的array和python中自帶的list之間相互轉化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
PyTorch實現(xiàn)手寫數(shù)字識別的示例代碼
本文主要介紹了PyTorch實現(xiàn)手寫數(shù)字識別的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下<BR>2022-05-05

