網站首頁 編程語言 正文
1. 簡介
有些時候在項目中,使用配置文件來配置一些靈活的參數是比較常見的事,因為這會使得代碼的維護變得更方便。而ini配置文件是比較常用的一種,今天介紹用ConfigParser模塊來解析ini配置文件。
2. ini配置文件格式
# 這是注釋 ; 這也是注釋 [section1] name = wang age = 18 heigth = 180 [section2] name = python age = 19
3. 讀取ini文件
configparser模塊為Python自帶模塊不需要單獨安裝,但要注意,在Python3中的導入方式與Python2的有點小區別
# python2 import ConfigParser # python3 import configparser
3.1 初始化對象并讀取文件
import configparser import os # 創建對象 config = configparser.ConfigParser() dirPath = os.path.dirname(os.path.realpath(__file__)) inipath = os.path.join(dirPath,'test.ini') # 讀取配置文件,如果配置文件不存在則創建 config.read(inipath,encoding='utf-8')
3.2 獲取并打印所有節點名稱
secs = config.sections() print(secs)
輸出結果:
['section1', 'section2']
3.3 獲取指定節點的所有key
option = config.options('section1') print(option)
輸出結果:
['name', 'age', 'heigth']
3.4 獲取指定節點的鍵值對
item_list = config.items('section2') print(item_list)
輸出結果:
[('name', 'python'), ('age', '19')]
3.5 獲取指定節點的指定key的value
val = config.get('section1','age') print('section1的age值為:',val)
輸出結果:
section1的age值為: 18
3.6 將獲取到值轉換為int\bool\浮點型
Attributes = config.getint('section2','age') print(type(config.get('section2','age'))) print(type(Attributes)) # Attributes2 = config.getboolean('section2','age') # Attributes3 = config.getfloat('section2','age')
輸出結果:
<class 'str'>
<class 'int'>
3.7 檢查section或option是否存在,返回bool值
has_sec = config.has_section('section1') print(has_sec) has_opt = config.has_option('section1','name') print(has_opt)
輸出結果:
TrueTrue
3.8 添加一個section和option
if not config.has_section('node1'): config.add_section('node1') # 不需判斷key存不存在,如果key不存在則新增,若已存在,則修改value config.set('section1','weight','100') # 將添加的節點node1寫入配置文件 config.write(open(inipath,'w')) print(config.sections()) print(config.options('section1'))
輸出結果:
['section1', 'section2', 'node1']
[('name', 'wang'), ('age', '18'), ('heigth', '180'), ('weight', '100')]
3.9 刪除section和option
# 刪除option print('刪除前的option:',config.items('node1')) config.remove_option('node1','dd') # 將刪除節點node1后的內容寫回配置文件 config.write(open(inipath,'w')) print('刪除后的option:',config.items('node1'))
輸出結果:
刪除前的option: [('dd', 'ab')]
刪除后的option: []
# 刪除section print('刪除前的section: ',config.sections()) config.remove_section('node1') config.write(open(inipath,'w')) print('刪除后的section: ',config.sections())
輸出結果:
刪除前的section: ?['section1', 'section2', 'node1']
刪除后的section: ?['section1', 'section2']
3.10 寫入方式
1、write寫入有兩種方式,一種是刪除源文件內容,重新寫入:w
config.write(open(inipath,'w'))
另一種是在原文基礎上繼續寫入內容,追加模式寫入:a
config.write(open(inipath,'a'))
需要注意的是,config.read(inipath,encoding='utf-8')
只是將文件內容讀取到內存中,即使經過一系列的增刪改操作,只有執行了以上的寫入代碼后,操作過的內容才會被寫回文件,才能生效。
原文鏈接:https://blog.csdn.net/weixin_38813807/article/details/128669736
相關推薦
- 2022-05-20 Kafka 認證登錄注意事項
- 2022-04-22 nvm切換node版本后提示npm : 無法將“npm”項識別為 cmdlet、函數、腳本文件或可運
- 2022-09-06 Minio設置文件鏈接永久有效的完整步驟_其它綜合
- 2022-07-19 CentOS8 服務器連接超時自動斷開問題解決
- 2022-08-25 C++淺析STL?迭代器?容器的使用_C 語言
- 2022-12-21 Python中Turtle庫改變畫筆(海龜)方向的兩種方法總結_python
- 2022-11-17 ios開發Flutter構建todo?list應用_IOS
- 2022-05-19 基于角色的權限控制模型RBAC圖文教程_相關技巧
- 最近更新
-
- 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同步修改后的遠程分支