前言:目前我們使用的絕大多數計算機程序,無論是辦公軟件,瀏覽器,甚至游戲、視頻都是通過菜單界面系統配置的,它幾乎成了我們使用機器的默認方式。而在python中,也有這樣的一個配置模塊可以把代碼可配置化。
什么是配置文件
?這里的配置文件不同于我們平常所見的可視化的菜單界面,它是像代碼形式的,如下示例:
?為什么要做配置文件?
?讓代碼和配置都變成可模塊化可配置化,提高代碼的重用性,那什么時候把它變成可配置化呢?多處地方都用到一個參數時,經常會變化的參數等,都可以可配置化,我們只需要在配置文件中進行修改即可,不需要在代碼中一處處的重復修改。
?Python提供了一個ConfigParser模塊,它實現了一種基本的配置文件解析器語言,該語言提供的結構類似于.ini文件中的結構。常見的配置文件格式有.ini.conf.cfg,配置文件由兩個文件對象組成:section和option,一個配置文件里可以包含一個或多個節(section),每個節可以有多個option(鍵=值),如上圖所標示。
讀取配置文件
?它與file文件一樣,需要先打開才能進行讀取操作,常用方法如下:
-
read(filename)
:直接讀取配置文件內容 -
sections()
:以列表的形式返回所有section -
options(section)
:得到對應section下的所有option -
items(section)
:得到對應section下的所有鍵值對 -
get(section,option)
:得到對應的section中的option的值,并以string的類型返回 -
getint(section,option)
:得到對應的section中的option的值,并以int的類型返回
?以上圖中的conf.ini為例進行讀取操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
from configparser import ConfigParser # 創建一個操作配置文件的對象(文件解析對象) conf = ConfigParser() # 讀取配置文件 conf.read( "conf.ini" , encoding = "utf8" ) # 獲取所有section res2 = conf.sections() print ( "這是res2:{}\n" . format (res2)) # 獲取對應section下的option res3 = conf.options( "logging" ) print ( "這是res3:{}\n" . format (res3)) # 獲取對應section下的所有鍵值對 res4 = conf.items( "logging" ) print ( "這是res4:{}\n" . format (res4)) # get方法:讀取出來的內容,都是字符串 res5 = conf.get( "logging" , "level" ) print ( "這是res5:{}" . format (res5), type (res5)) # getint方法:讀取出來的內容,都是int類型 res6 = conf.getint( "mysql" , "port" ) print ( "\n這是res6:{}" . format (res6), type (res6)) |
?運行結果:
C:\software\python\python.exe D:/learn/test.py
這是res2:['logging', 'mysql']這是res3:['level', 'f_level', 's_level']
這是res4:[('level', 'DEBUG'), ('f_level', 'DEBUG'), ('s_level', 'ERROR')]
這是res5:DEBUG <class 'str'>
這是res6:3306 <class 'int'>
Process finished with exit code 0
?除了可以讀取str、int類型以外,還支持float、boolean,這里就不再舉例。
? 小知識:
-
鍵值對可用
=
也可用:
進行分隔 -
section
名稱是區分大小寫的,而option不區分 - 鍵值對中,首尾若有空白符會被去掉
-
配置文件中也可以寫入注釋,注釋以
#
或者;
為前綴
寫入配置文件
?基本的寫入方法如下:
add_section(section)
:添加一個新的sectionset( section, option, value)
:對section中的option進行設置,需要調用write將內容寫入配置文件
1
2
3
4
5
6
7
|
from configparser import ConfigParser # 創建一個操作配置文件的對象(文件解析對象) conf = ConfigParser() conf.add_section( 'test' ) conf.set( 'test' , 'name' , 'Amy' ) conf.write(open( 'conf.ini' , "a" , encoding= "utf-8" )) |
?運行后查看conf.ini文件里面的內容:
ConfigParser的封裝
?一次封裝,一勞永逸,之后直接調用即可,封裝內容按需。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
from configparser import ConfigParser class MyConf: def __init__(self, filename, encoding= "utf8" ): self.filename = filename self.encoding = encoding self.conf = ConfigParser() self.conf.read(filename, encoding) def get_str(self, section, option): return self.conf.get(section, option) def get_int(self, section, option): return self.conf.getint(section, option) def get_float(self, section, option): return self.conf.getfloat(section, option) def get_bool(self, section, option): def write_data(self, section, option, value): self.conf.set(section, option, value) self.conf.write(open(self.filename, "a" , encoding=self.encoding)) if __name__ == '__main__' : print(conf.get_str( "conf.ini" , "test" , "name" )) # 測試 |
總結
到此這篇關于python接口自動化 ConfigParser配置文件的使用的文章就介紹到這了,更多相關python接口自動化ConfigParser配置文件內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/miki-peng/p/13419730.html