ConfigParser模塊在python中用來讀取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一個或多個節(section)。每個節可以有多個參數(鍵=值)。使用的配置文件的好處就是不用在程序員寫死,可以使程序更靈活。
注意:在python3中ConfigParser模塊名已更名爲configparser
configparser函數常用方法:
讀取配置文件:
read(filename) #讀取配置文件,直接讀取ini文件內容 sections() #獲取ini文件內所有的section,以列表形式返回['logging', 'mysql'] options(sections) #獲取指定sections下所有options ,以列表形式返回['host', 'port', 'user', 'password'] items(sections) #獲取指定section下所有的鍵值對,[('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('password', '123456')] get(section, option) #獲取section中option的值,返回爲string類型
更多詳細內容請看:https://www.cnblogs.com/lhly/p/8066898.html