學會使用Python?Configparser處理ini文件模塊
Configparser 模塊
本文知識點主要來源于網絡上的文章,比較分散就沒有放文章鏈接了~
學習路徑
Configparser 模塊簡介;
- Configparser 模塊安裝;
使用 Configparser 模塊;
1. Configparser 模塊簡介;
在 python 日常開發(fā)工作當中,我們可以用 py 文件、ini、xml、excel、yaml、json、toml 等來管理我們的配置信息,伴隨而來的是對這些文件處理的方法,Configparser 模塊就是用來處理 ini 文件的模塊。
2. Configparser 模塊安裝;
pip3 install configparser
安裝超級快,說明模塊很小~
方法
除了一些系統(tǒng)定義的函數(shù)和一些常量,也就這么些方法,不多。接下來我們一個一個用用看~
3. 使用 Configparser 模塊;
代碼演示:
import configparser import os def test(): # 初始化實例并讀取配置文件: config_parser = configparser.ConfigParser() config_parser.read("config.ini", encoding="utf-8") print("獲取所用 section 節(jié)點:", config_parser.sections()) print("獲取指定 section 的 options,即將配置文件中的某個 section 內的所有 key 讀取到列表中:", config_parser.options("section1")) print("判斷配置文件中是否有某個 section:", config_parser.has_section("demo")) print("判斷某個 section 下是否有某個 option:", config_parser.has_option("section1", "age")) print("判斷某個 section 下是否有某個 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ù),否則報錯):", config_parser.getint("section1", "age")) print("獲取指定 section 的指定 option(值必須是浮點型數(shù)據(jù),否則報錯):", config_parser.getfloat("section1", "weight")) print("獲取指定 section 的指定 option(值必須是 boolean 值型數(shù)據(jù),否則報錯):", 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")) # 高級用法 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")) # 高級用法 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")) # 高級用法 3:從文件對象中讀取配置 with open('config.ini') as config_file: config_parser.read_file(config_file) print("從文件對象中讀取配置:", config_parser.get("section1", "height")) config_file.close() # 高級用法 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í)行代碼演示:
至此,我們對 Configparser 模塊的使用已經基本探索完畢,其接口簡單,相對全面,是讀取 ini 配置文件的好幫手~
以上就是學會使用Python Configparser處理ini文件模塊的詳細內容,更多關于Python Configparser處理ini的資料請關注腳本之家其它相關文章!
相關文章
Python雙向循環(huán)鏈表實現(xiàn)方法分析
這篇文章主要介紹了Python雙向循環(huán)鏈表,結合實例形式分析了Python雙向鏈表的定義、遍歷、添加、刪除、搜索等相關操作技巧,需要的朋友可以參考下2018-07-07Python中使用dwebsocket實現(xiàn)后端數(shù)據(jù)實時刷新
dwebsocket是Python中一款用于實現(xiàn)WebSocket協(xié)議的庫,可用于后端數(shù)據(jù)實時刷新。在Django中結合使用dwebsocket和Channels,可以實現(xiàn)前后端的實時通信,支持雙向數(shù)據(jù)傳輸和消息推送,適用于實時聊天、數(shù)據(jù)監(jiān)控、在線游戲等場景2023-04-04