網站首頁 編程語言 正文
1 configparser安裝
pip3 install configparser
2 configparser簡介
用來讀取配置文件的python包;
一般做自動化測試的時候,會使用到這個模塊,用來封裝一些常量。比如數據庫、郵件、用戶名密碼、項目常量等等;
這個使用根據個人喜好和項目來確定,不一定一定要使用這個模塊,也可以使用其它的方法做配置,比如py文件、xml、excel、yaml、json等等。
configparser源碼大約1360行左右,通讀源碼可有效了解該模塊的使用。本文只做簡單介紹常用的方法。
3 表示方法
- 新建一個名為
conf.py
文件; - 寫入如下數據,格式如下:
[mysqldb] sql_host = 127.0.0.1 sql_port = 3699 sql_user = root sql_pass = 123456 [mailinfo] name = NoamaNelson passwd = 123456 address = 123456@qq.com
4 configparser詳細使用
4.1 對象初始化
寫入如下代碼,把對象初始化。
# -*- coding:utf-8 -*- # 作者:NoamaNelson # 日期:2021/11/19 # 文件名稱:conf.py # 作用:configparser模塊的使用 # 聯系:VX(NoamaNelson) # 博客:https://blog.csdn.net/NoamaNelson import configparser import os class Conf: def __init__(self): self.conf = configparser.ConfigParser() self.root_path = os.path.dirname(os.path.abspath(__file__)) self.f = os.path.join(self.root_path + "/config.conf") self.conf.read(self.f)
4.2 獲取所有的sections
def read_sections(self): print(f"1、獲取所有的sections:{self.conf.sections()}") if __name__ == "__main__": aa = Conf() aa.read_sections()
結果為:
D:\Python37\python.exe F:/python_study/conf.py
1、獲取所有的sections:['mysqldb', 'mailinfo']
4.3 獲取所有的sections對應的options
def read_options(self, s1, s2): print(f"2、獲取mysqldb所有的options:{self.conf.options(s1)}") print(f"3、獲取mailinfo所有的options:{self.conf.options(s2)}") if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo")
結果為:
D:\Python37\python.exe F:/python_study/conf.py
1、獲取所有的sections:['mysqldb', 'mailinfo']
2、獲取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass']
3、獲取mailinfo所有的options:['name', 'passwd', 'address']
4.4 read方法和get方法,獲取指定section下的option值
-
read
是讀取配置文件,如?self.conf.read(self.f)
是讀取指定config.conf
文件; -
get
是獲取具體的值;
def read_conf(self, m, n): name = self.conf.get(m, n) # 獲取指定section的option值 print(f"4、獲取指定section:{m}下的option:{n}的值為{name}") if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host")
結果為:
D:\Python37\python.exe F:/python_study/conf.py
1、獲取所有的sections:['mysqldb', 'mailinfo']
2、獲取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass']
3、獲取mailinfo所有的options:['name', 'passwd', 'address']
4、獲取指定section:mysqldb下的option:sql_host的值為127.0.0.1
4.5 items方法,獲取指點section所用配置信息
def get_items(self, m, n): print(f"5、獲取sectoion:{m}下的配置信息為:{self.conf.items(m)}") print(f"6、獲取sectoion:{n}下的配置信息為:{self.conf.items(n)}") if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo")
結果為:
D:\Python37\python.exe F:/python_study/conf.py
1、獲取所有的sections:['mysqldb', 'mailinfo']
2、獲取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass']
3、獲取mailinfo所有的options:['name', 'passwd', 'address']
4、獲取指定section:mysqldb下的option:sql_host的值為127.0.0.1
5、獲取sectoion:mysqldb下的配置信息為:[('sql_host', '127.0.0.1'), ('sql_port', '3699'), ('sql_user', 'root'), ('sql_pass', '123456')]
6、獲取sectoion:mailinfo下的配置信息為:[('name', 'NoamaNelson'), ('passwd', '123456'), ('address', '123456@qq.com')]
4.6 set和write方法,修改某個option的值
如果option不存在則創建它;
def set_option(self, m, n, s): self.conf.set(m, n, s) self.conf.write(open(self.f, "w")) print(f"7、設置setion:{m}下的option:{n}的值為:{s}") if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo") aa.set_option("mysqldb", "sql_name", "游客")
結果為:
D:\Python37\python.exe F:/python_study/conf.py
1、獲取所有的sections:['mysqldb', 'mailinfo']
2、獲取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass']
3、獲取mailinfo所有的options:['name', 'passwd', 'address']
4、獲取指定section:mysqldb下的option:sql_host的值為127.0.0.1
5、獲取sectoion:mysqldb下的配置信息為:[('sql_host', '127.0.0.1'), ('sql_port', '3699'), ('sql_user', 'root'), ('sql_pass', '123456')]
6、獲取sectoion:mailinfo下的配置信息為:[('name', 'NoamaNelson'), ('passwd', '123456'), ('address', '123456@qq.com')]
7、設置setion:mysqldb下的option:sql_name的值為:游客
因為之前的配置文件中沒有sql_name
,所以會新創建它,如果創建的內容存在,直接修改對應的值;
4.7 has_section和has_option方法
-
has_section
方法檢查對應的section
是否存在; -
has_option
方法檢查對應的option
是否存在;
def has_s_o(self, s, o): print(f"8、檢查section:{s}是否存在:{self.conf.has_section(s)}") print(f"9、檢查section:{s}下的option:{o}是否存在:{self.conf.has_option(s, o)}") if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo") aa.set_option("mysqldb", "sql_name", "游客") aa.has_s_o("mysqldb", "sql_name")
結果為:
D:\Python37\python.exe F:/python_study/conf.py
1、獲取所有的sections:['mysqldb', 'mailinfo']
2、獲取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass', 'sql_name']
3、獲取mailinfo所有的options:['name', 'passwd', 'address']
4、獲取指定section:mysqldb下的option:sql_host的值為127.0.0.1
5、獲取sectoion:mysqldb下的配置信息為:[('sql_host', '127.0.0.1'), ('sql_port', '3699'), ('sql_user', 'root'), ('sql_pass', '123456'), ('sql_name', '游客')]
6、獲取sectoion:mailinfo下的配置信息為:[('name', 'NoamaNelson'), ('passwd', '123456'), ('address', '123456@qq.com')]
7、設置setion:mysqldb下的option:sql_name的值為:游客
8、檢查section:mysqldb是否存在:True
9、檢查section:mysqldb下的option:sql_name是否存在:True
4.8 add_section方法,添加section和option
add_section
:添加section
;
添加前:
def add_s_o(self, s, o, v): if not self.conf.has_section(s): self.conf.add_section(s) print(f"10、添加新的section為{s}") else: print(f"10、添加新的section為{s}已經存在,無需添加!") if not self.conf.has_option(s, o): self.conf.set(s, o, v) print(f"11、要添加的option為{o}, 值為{v}") else: print(f"11、要添加的option為{o}, 值為{v},已經存在,無需添加!") self.conf.write(open(self.f, "w")) if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo") aa.set_option("mysqldb", "sql_name", "游客") aa.has_s_o("mysqldb", "sql_name") aa.add_s_o("login", "name", "root")
添加后:
D:\Python37\python.exe F:/python_study/conf.py
1、獲取所有的sections:['mysqldb', 'mailinfo']
2、獲取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass', 'sql_name']
3、獲取mailinfo所有的options:['name', 'passwd', 'address']
4、獲取指定section:mysqldb下的option:sql_host的值為127.0.0.1
5、獲取sectoion:mysqldb下的配置信息為:[('sql_host', '127.0.0.1'), ('sql_port', '3699'), ('sql_user', 'root'), ('sql_pass', '123456'), ('sql_name', '游客')]
6、獲取sectoion:mailinfo下的配置信息為:[('name', 'NoamaNelson'), ('passwd', '123456'), ('address', '123456@qq.com')]
7、設置setion:mysqldb下的option:sql_name的值為:游客
8、檢查section:mysqldb是否存在:True
9、檢查section:mysqldb下的option:sql_name是否存在:True
10、添加新的section為login
11、要添加的option為name, 值為root
再次運行代碼,會提示已經存在:
10、添加新的section為login已經存在,無需添加!
11、要添加的option為name, 值為root,已經存在,無需添加!
4.9 remove_section和remove_option方法,刪除section和option
def remove_s_o(self, s, o): if self.conf.has_section(s): self.conf.remove_section(s) print(f"12、刪除section:{s}==OK!") else: print(f"12、要刪除的section:{s}不存在,不用刪除!") if self.conf.has_option(s, o): self.conf.remove_option(s, o) print(f"13、刪除section:{s}下的option:{o}==OK!") else: print(f"13、要刪除的section:{s}下的option:{o}不存在,不用刪除!") if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo") aa.set_option("mysqldb", "sql_name", "游客") aa.has_s_o("mysqldb", "sql_name") aa.add_s_o("login", "name", "root") aa.remove_s_o("login", "name")
結果為:
D:\Python37\python.exe F:/python_study/conf.py
1、獲取所有的sections:['mysqldb', 'mailinfo', 'login']
2、獲取mysqldb所有的options:['sql_host', 'sql_port', 'sql_user', 'sql_pass', 'sql_name']
3、獲取mailinfo所有的options:['name', 'passwd', 'address']
4、獲取指定section:mysqldb下的option:sql_host的值為127.0.0.1
5、獲取sectoion:mysqldb下的配置信息為:[('sql_host', '127.0.0.1'), ('sql_port', '3699'), ('sql_user', 'root'), ('sql_pass', '123456'), ('sql_name', '游客')]
6、獲取sectoion:mailinfo下的配置信息為:[('name', 'NoamaNelson'), ('passwd', '123456'), ('address', '123456@qq.com')]
7、設置setion:mysqldb下的option:sql_name的值為:游客
8、檢查section:mysqldb是否存在:True
9、檢查section:mysqldb下的option:sql_name是否存在:True
10、添加新的section為login已經存在,無需添加!
11、要添加的option為name, 值為root,已經存在,無需添加!
12、刪除section:login==OK!
13、要刪除的section:login下的option:name不存在,不用刪除!
5 configparser常見的異常
異常 | 描述 |
---|---|
ConfigParser.Error | 所有異常的基類 |
ConfigParser.NoSectionError | 指定的section沒有找到 |
ConfigParser.DuplicateSectionError | 調用add_section() 時,section名稱已經被使用 |
ConfigParser.NoOptionError | 指定的參數沒有找到 |
ConfigParser.InterpolationError | 當執行字符串插值時出現問題時,出現異常的基類 |
ConfigParser.InterpolationDepthError | 當字符串插值無法完成時,因為迭代次數超過了最大的范圍,所以無法完成。InterpolationError的子類 |
InterpolationMissingOptionError | 當引用的選項不存在時,會出現異常。InterpolationError的子類 |
ConfigParser.InterpolationSyntaxError | 當產生替換的源文本不符合所需的語法時,就會出現異常。InterpolationError的子類。 |
ConfigParser.MissingSectionHeaderError | 當試圖解析一個沒有分段標題的文件時,會出現異常。 |
ConfigParser.ParsingError | 當試圖解析文件時發生錯誤時,會出現異常 |
ConfigParser.MAX_INTERPOLATION_DEPTH | 當raw參數為false時,get()的遞歸插值的最大深度。這只適用于ConfigParser類 |
6 本文所有源碼
# -*- coding:utf-8 -*- # 作者:NoamaNelson # 日期:2021/11/19 # 文件名稱:conf.py # 作用:configparser模塊的使用 # 聯系:VX(NoamaNelson) # 博客:https://blog.csdn.net/NoamaNelson import configparser import os class Conf: def __init__(self): self.conf = configparser.ConfigParser() self.root_path = os.path.dirname(os.path.abspath(__file__)) self.f = os.path.join(self.root_path + "/config.conf") self.conf.read(self.f) def read_sections(self): print(f"1、獲取所有的sections:{self.conf.sections()}") def read_options(self, s1, s2): print(f"2、獲取mysqldb所有的options:{self.conf.options(s1)}") print(f"3、獲取mailinfo所有的options:{self.conf.options(s2)}") def read_conf(self, m, n): name = self.conf.get(m, n) # 獲取指定section的option值 print(f"4、獲取指定section:{m}下的option:{n}的值為{name}") def get_items(self, m, n): print(f"5、獲取sectoion:{m}下的配置信息為:{self.conf.items(m)}") print(f"6、獲取sectoion:{n}下的配置信息為:{self.conf.items(n)}") def set_option(self, m, n, s): self.conf.set(m, n, s) self.conf.write(open(self.f, "w")) print(f"7、設置setion:{m}下的option:{n}的值為:{s}") def has_s_o(self, s, o): print(f"8、檢查section:{s}是否存在:{self.conf.has_section(s)}") print(f"9、檢查section:{s}下的option:{o}是否存在:{self.conf.has_option(s, o)}") def add_s_o(self, s, o, v): if not self.conf.has_section(s): self.conf.add_section(s) print(f"10、添加新的section為{s}") else: print(f"10、添加新的section為{s}已經存在,無需添加!") if not self.conf.has_option(s, o): self.conf.set(s, o, v) print(f"11、要添加的option為{o}, 值為{v}") else: print(f"11、要添加的option為{o}, 值為{v},已經存在,無需添加!") self.conf.write(open(self.f, "w")) def remove_s_o(self, s, o): if self.conf.has_section(s): self.conf.remove_section(s) print(f"12、刪除section:{s}==OK!") else: print(f"12、要刪除的section:{s}不存在,不用刪除!") if self.conf.has_option(s, o): self.conf.remove_option(s, o) print(f"13、刪除section:{s}下的option:{o}==OK!") else: print(f"13、要刪除的section:{s}下的option:{o}不存在,不用刪除!") if __name__ == "__main__": aa = Conf() aa.read_sections() aa.read_options("mysqldb", "mailinfo") aa.read_conf("mysqldb", "sql_host") aa.get_items("mysqldb", "mailinfo") aa.set_option("mysqldb", "sql_name", "游客") aa.has_s_o("mysqldb", "sql_name") aa.add_s_o("login", "name", "root") aa.remove_s_o("login", "name")
原文鏈接:https://segmentfault.com/a/1190000043341874
相關推薦
- 2022-09-12 Python詳解復雜CSV文件處理方法_python
- 2023-01-28 架構師說比起404我們更怕200錯誤_相關技巧
- 2022-06-01 C語言?深入淺出講解指針的使用_C 語言
- 2022-12-03 Python學習之列表和元組的使用詳解_python
- 2023-01-20 C++利用模板實現消息訂閱和分發功能_C 語言
- 2022-12-10 C++中如何將數據保存為CSV文件_C 語言
- 2023-05-20 Golang基于文件魔數判斷文件類型的案例代碼_Golang
- 2022-06-14 Qt拖放操作和打印操作的實現_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支