學(xué)會(huì)使用Python?Configparser處理ini文件模塊
Configparser 模塊
本文知識(shí)點(diǎn)主要來源于網(wǎng)絡(luò)上的文章,比較分散就沒有放文章鏈接了~
學(xué)習(xí)路徑
Configparser 模塊簡(jiǎn)介;
- Configparser 模塊安裝;
使用 Configparser 模塊;
1. Configparser 模塊簡(jiǎn)介;
在 python 日常開發(fā)工作當(dāng)中,我們可以用 py 文件、ini、xml、excel、yaml、json、toml 等來管理我們的配置信息,伴隨而來的是對(duì)這些文件處理的方法,Configparser 模塊就是用來處理 ini 文件的模塊。
2. Configparser 模塊安裝;
pip3 install configparser
安裝超級(jí)快,說明模塊很小~
方法
除了一些系統(tǒng)定義的函數(shù)和一些常量,也就這么些方法,不多。接下來我們一個(gè)一個(gè)用用看~
3. 使用 Configparser 模塊;
代碼演示:
import configparser import os def test(): # 初始化實(shí)例并讀取配置文件: config_parser = configparser.ConfigParser() config_parser.read("config.ini", encoding="utf-8") print("獲取所用 section 節(jié)點(diǎn):", config_parser.sections()) print("獲取指定 section 的 options,即將配置文件中的某個(gè) section 內(nèi)的所有 key 讀取到列表中:", config_parser.options("section1")) print("判斷配置文件中是否有某個(gè) section:", config_parser.has_section("demo")) print("判斷某個(gè) section 下是否有某個(gè) option:", config_parser.has_option("section1", "age")) print("判斷某個(gè) section 下是否有某個(gè) option:", config_parser.has_option("section1", "developer")) print("獲取指定 section 的指定 option(值是字符串或其他數(shù)字類型都可以):", config_parser.get("section1", "name")) print("獲取指定 section 的指定 option(值是字符串或其他數(shù)字類型都可以):", config_parser.get("section1", "age")) print("獲取指定 section 的指定 option(值是字符串或其他數(shù)字類型都可以):", config_parser.get("section2", "name")) print("獲取指定 section 的指定 option(值必須是整數(shù)型數(shù)據(jù),否則報(bào)錯(cuò)):", config_parser.getint("section1", "age")) print("獲取指定 section 的指定 option(值必須是浮點(diǎn)型數(shù)據(jù),否則報(bào)錯(cuò)):", config_parser.getfloat("section1", "weight")) print("獲取指定 section 的指定 option(值必須是 boolean 值型數(shù)據(jù),否則報(bào)錯(cuò)):", config_parser.getboolean("section1", "student")) # 添加 section 和 option config_parser.add_section("section3") config_parser.set("section3", "name", "baby") config_parser.write(open("config.ini", "w")) # 刪除 section 和 option config_parser.remove_option("section3", "name") config_parser.remove_section("section3") config_parser.write(open("config.ini", "w")) # 高級(jí)用法 1:從字典中讀取配置 config_dict = { "oracle": { "hostname": "127.0.0.1", "username": "admin", "password": "admin123" } } config_parser.read_dict(config_dict) print("從字典中讀取配置:", config_parser.get("oracle", "hostname")) # 高級(jí)用法 2:從字符串中讀取配置 config_str = """ [section4] version = 1.0.1 author = dylan.zhang date = 2023.3.14 """ config_parser.read_string(config_str) print("從字符串中讀取配置:", config_parser.get("section4", "version")) # 高級(jí)用法 3:從文件對(duì)象中讀取配置 with open('config.ini') as config_file: config_parser.read_file(config_file) print("從文件對(duì)象中讀取配置:", config_parser.get("section1", "height")) config_file.close() # 高級(jí)用法 4:讀取 section 并返回可迭代的列表 config_items = config_parser.items("section1") for key, value in config_items: print("迭代讀取 section1 配置:", key, value) for index, value in enumerate(config_items): print("迭代讀取 section1 配置:", index, value) if __name__ == '__main__': test()
執(zhí)行代碼演示:
至此,我們對(duì) Configparser 模塊的使用已經(jīng)基本探索完畢,其接口簡(jiǎn)單,相對(duì)全面,是讀取 ini 配置文件的好幫手~
以上就是學(xué)會(huì)使用Python Configparser處理ini文件模塊的詳細(xì)內(nèi)容,更多關(guān)于Python Configparser處理ini的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
68行Python代碼實(shí)現(xiàn)帶難度升級(jí)的貪吃蛇
本文主要介紹了Python代碼實(shí)現(xiàn)帶難度升級(jí)的貪吃蛇,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Python雙向循環(huán)鏈表實(shí)現(xiàn)方法分析
這篇文章主要介紹了Python雙向循環(huán)鏈表,結(jié)合實(shí)例形式分析了Python雙向鏈表的定義、遍歷、添加、刪除、搜索等相關(guān)操作技巧,需要的朋友可以參考下2018-07-07Python中使用dwebsocket實(shí)現(xiàn)后端數(shù)據(jù)實(shí)時(shí)刷新
dwebsocket是Python中一款用于實(shí)現(xiàn)WebSocket協(xié)議的庫,可用于后端數(shù)據(jù)實(shí)時(shí)刷新。在Django中結(jié)合使用dwebsocket和Channels,可以實(shí)現(xiàn)前后端的實(shí)時(shí)通信,支持雙向數(shù)據(jù)傳輸和消息推送,適用于實(shí)時(shí)聊天、數(shù)據(jù)監(jiān)控、在線游戲等場(chǎng)景2023-04-04Python讀寫二進(jìn)制文件的實(shí)現(xiàn)
本文主要介紹了Python讀寫二進(jìn)制文件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04