Python ini文件常用操作方法解析
一、config.ini 配置文件
[DATABASE]
host = 192.1.1.1
username = root
password = root
port = 3306
database = jforum[URL]
#ip,端口
ip =127.0.0.1
port= 8089
二、操作ini常用方法
--read():讀取配置文件
--sections():讀取配置文件中所有的section(如上配置文件:DATABASE,URL)
--options(section):讀取該section下所有的option(可以理解成讀取該組下的所有key,如options("URL"),值['ip', 'port'])
--items(section):讀取該section下的所有key-vaule,并以鍵值對(duì)形式輸出(如:sectioitems("URL"),值:[('ip', '127.0.0.1'), ('port', '8089')])
--get(section, option):讀取指定section下面的option的值(可以理解成,讀取具體某個(gè)section下面指定key的值,如config.get('URL','ip')),值:127.0.0.1)
--add_section(section):添加一個(gè)section,參數(shù)為section的名稱
--set(section, option, value):在section下面添加一條數(shù)據(jù)(key=value)
--add與set需調(diào)用write(open(configPath, "a"))才可以寫入ini文件 #參數(shù)a表示最近,w重寫
--remove_seciton(seciton) 刪除整個(gè)seciton
--config.remove_option(seciton,key) ,刪除seciton的某個(gè)key值
三、源碼舉例
#!/usr/bin/python3
# encoding:utf-8
'''
Created on 2020-04-19 23:19
@author: Administrator
'''
import configparser
import os
from turtle import readconfig
#獲取文件絕對(duì)路徑 D:\common\
proDir = os.getcwd()
#拼接文件路徑 D:\common\config.ini
configPath = os.path.join(proDir, "config.ini")
#創(chuàng)建管理對(duì)象
config = configparser.ConfigParser()
#讀取配置類
class readConfig():
#讀取ini文件
config.read(configPath, encoding="UTF-8")
#獲取所有的section
@staticmethod
def get_sections():
return config.sections()
@staticmethod
def get_items(section):
return config.items(section)
@staticmethod
def get_options(section):
return config.options(section)
@staticmethod
def get_Vaule(section,name):
value = config.get(section, name)
return value
@staticmethod
def add_section():
config.add_section('HTTP')
@staticmethod
def set_section(section, option, value):
config.set(section, option, value)
@staticmethod
def remove_seciton(seciton):
config.remove_section(seciton)
@staticmethod
def remove_seciton_value(seciton,key):
config.remove_option(seciton,key)
if __name__=='__main__':
print('-----1.打印所有section')
print(readConfig.get_sections())
print('-----2.打印section=URL的所有key-Value值')
print(readConfig.get_items("URL"))
print('-----3.打印section=URL的所有key值')
print(readConfig.get_options("URL"))
print('-----4.打印section=URL,key=ip的value值')
print(readConfig.get_Vaule('URL','ip'))
print('-----5.新增之后打印所有section,注意有一個(gè)新增值HTTP')
readConfig.add_section()
print(readConfig.get_sections())
print('-----6.新增section=HTTP,key=port,value=443,查看值,443為新增的值')
readConfig.set_section('HTTP', 'port', '443')
print(readConfig.get_Vaule('HTTP','port'))
#上面的新增并不會(huì)真的真正寫入,需加這個(gè)才能正在寫入ini文件,如果參數(shù)為"w"則表示刪除文件重新寫入,"a"為追加模式寫入
#config.write(open(configPath, "a"))
print('-----7.刪除sections=URL,打印所有sections,注意URL已被刪除')
readConfig.remove_seciton("URL")
print(readConfig.get_sections())
print('-----8.刪除sections=DATABASE,key=host,打印所有key值,注意host已被刪除')
readConfig.remove_seciton_value('DATABASE','host')
print(readConfig.get_options('DATABASE'))
運(yùn)行結(jié)果
-----1.打印所有section
['DATABASE', 'URL']
-----2.打印section=URL的所有key-Value值
[('ip', '127.0.0.1'), ('port', '8089')]
-----3.打印section=URL的所有key值
['ip', 'port']
-----4.打印section=URL,key=ip的value值
127.0.0.1
-----5.新增之后打印所有section,注意有一個(gè)新增值HTTP
['DATABASE', 'URL', 'HTTP']
-----6.新增section=HTTP,key=port,value=443,查看值,443為新增的值
443
-----7.刪除sections=URL,打印所有sections,注意URL已被刪除
['DATABASE', 'HTTP']
-----8.刪除sections=DATABASE,key=host,打印所有key值,注意host已被刪除
['username', 'password', 'port', 'database']
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python操作ini類型配置文件的實(shí)例教程
- 淺談Python __init__.py的作用
- python讀取配置文件方式(ini、yaml、xml)
- Python讀取配置文件(config.ini)以及寫入配置文件
- python 非線性規(guī)劃方式(scipy.optimize.minimize)
- python構(gòu)造函數(shù)init實(shí)例方法解析
- python讀取ini配置的類封裝代碼實(shí)例
- Python包,__init__.py功能與用法分析
- python讀取ini配置文件過程示范
- python中對(duì)_init_的理解及實(shí)例解析
- Python3讀寫ini配置文件的示例
相關(guān)文章
如何利用python實(shí)現(xiàn)kmeans聚類
K-Means是聚類算法的一種,以距離來判斷數(shù)據(jù)點(diǎn)間的相似度并對(duì)數(shù)據(jù)進(jìn)行聚類,下面這篇文章主要給大家介紹了關(guān)于如何利用python實(shí)現(xiàn)kmeans聚類的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
python利用遞歸方法實(shí)現(xiàn)求集合的冪集
這篇文章主要給大家介紹了關(guān)于python利用遞歸方法實(shí)現(xiàn)求集合的冪集的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Python實(shí)現(xiàn)兩款計(jì)算器功能示例
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)兩款計(jì)算器功能示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
pycharm-professional-2020.1下載與激活的教程
這篇文章主要介紹了pycharm-professional-2020.1下載與激活的教程,本文分為安裝和永久激活兩部分內(nèi)容,需要的朋友可以參考下2020-09-09
Python訪問PostgreSQL數(shù)據(jù)庫詳細(xì)操作
postgresql是常用的關(guān)系型數(shù)據(jù)庫,并且postgresql目前還保持著全部開源的狀態(tài),這篇文章主要給大家介紹了關(guān)于Python訪問PostgreSQL數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2023-11-11
CodeWhisperer基于python使用經(jīng)驗(yàn)分享
這篇文章主要為大家介紹了CodeWhisperer基于python使用經(jīng)驗(yàn)分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
python實(shí)現(xiàn)定時(shí)發(fā)送qq消息
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)定時(shí)發(fā)送qq消息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01

