python操作ini類型配置文件的實例教程
一、ini文件介紹
INI文件格式是某些平臺或軟件上的配置文件的非正式標準,以節(jié)(section)和鍵(key)構(gòu)成,常用于微軟Windows操作系統(tǒng)中。這種配置文件的文件擴展名多為INI
二、ini文件的結(jié)構(gòu)
- 片段[section]
- 鍵名 option
- 值 value
三、實例:
實例1
python25.ini
[teachers]
name = ['yushen', 'pianpian']
age = 16
gender = '女'
favor = {"movie": "追風(fēng)", "music": "周杰倫"}
[student]
name = ['啦啦迷弟', '啦啦迷妹']
age = 18
操作ini文件
from configparser import ConfigParser
# 初始化
config = ConfigParser()
# 讀取文件
config.read('python25.ini', encoding='utf-8')
a = config.get('teachers', 'name')
print(a)
print(type(a))
運行結(jié)果如下:

實例2
fz.ini

讀取fz.ini文件代碼:
import configparser
import os
curpath = os.path.dirname(os.path.realpath(__file__))
cfgpath = os.path.join(curpath, "fz.ini")
# fz.ini的路徑
print(cfgpath)
# 創(chuàng)建管理對象
conf = configparser.ConfigParser()
# 讀ini文件
conf.read(cfgpath, encoding="utf-8")
# 獲取所有的section
sections = conf.sections()
# 返回list
print(sections)
items = conf.items('oracle')
# list里面對象是元祖
print(items)
運行結(jié)果:

實例3,封裝升級
set修改,add添加,write寫入、remove刪除
此封裝實現(xiàn)以下功能:
- 獲取sections列表
- 獲取指定的section的options列表
- 獲取指定section的配置信息列表
- 按類型讀取配置信息
- 新增section
- 設(shè)置指定option值
- 刪除指定section
- 刪除指定option
# -*- coding:utf-8 -*-
from configparser import ConfigParser
import os
class TEINI:
def __init__(self, path):
self.path = path
self.ini = ConfigParser()
self.ini.read(self.path)
# 獲取sections列表
def get_sections(self):
if self.ini:
return self.ini.sections()
# 獲取指定的section的options列表
def get_options_by_section(self, section):
if self.ini:
return self.ini.options(section)
# 獲取指定section的配置信息列表
def get_section_items(self, section):
if self.ini:
return self.ini.items(section)
# 按類型讀取配置信息
# 返回字符串類型
def get_string(self, section, option):
if self.ini:
return self.ini.get(section, option)
# 返回int類型
def get_int(self, section, option):
if self.ini:
return self.ini.getint(section, option)
# 返回float類型
def get_float(self, section, option):
if self.ini:
return self.ini.getfloat(section, option)
# 返回bool類型
def get_boolean(self, section, option):
if self.ini:
return self.ini.getboolean(section, option)
# 新增section
def add_section(self, section):
if self.ini:
self.ini.add_section(section)
self.ini.write(open(self.path, "w"))
# 設(shè)置指定option值
def set_option(self, section, option, value):
if self.ini:
self.ini.set(section, option, value)
self.ini.write(open(self.path, "w"))
# 刪除指定section
def remove_section(self, section):
if self.ini:
self.ini.remove_section(section)
self.ini.write(open(self.path, "w"))
# 刪除指定option
def remove_option(self, section, option):
if self.ini:
self.ini.remove_option(section, option)
self.ini.write(open(self.path, "w"))
if __name__ == "__main__":
print("python ini標準庫解析實例======根據(jù)需求運行代碼?。?!")
# 如果存在mysql.ini先刪除,方便下列代碼的運行
if os.path.exists("mysql.ini"):
os.remove("mysql.ini")
# 我們先寫一些數(shù)據(jù)到mysql.ini中
ini = TEINI("mysql.ini")
# 先加一個mysql section
mysql_section = "mysql"
ini.add_section(mysql_section)
# 在mysql section下寫入一些配置信息
ini.set_option(mysql_section, "host", "192.168.3.1")
ini.set_option(mysql_section, "port", "3306")
ini.set_option(mysql_section, "db", "mysql")
ini.set_option(mysql_section, "user", "admin")
ini.set_option(mysql_section, "password", "111111")
# 再添加一個oracle section
oracle_section = "oracle"
ini.add_section(oracle_section)
# oracle section下寫入一些配置信息
ini.set_option(oracle_section, "host", "192.172.0.1")
ini.set_option(oracle_section, "port", "8080")
ini.set_option(oracle_section, "db", "oracle")
ini.set_option(oracle_section, "user", "guiyin")
ini.set_option(oracle_section, "password", "666666")
# 獲取下所有的section,并在console輸出
sections = ini.get_sections()
print(sections)
# 遍歷各個section下的options,并在console中輸出
print("===" * 20)
for sec in sections:
print(sec, " 中的options為: ")
options = ini.get_options_by_section(sec)
print(options)
print("===" * 20)
# 獲取各個section下的配置信息
for sec in sections:
print(sec, " 中的配置信息為: ")
items = ini.get_section_items(sec)
print(items)
print("***" * 20)
# 獲取指定的option值這里演示讀取host和port
host = ini.get_string("mysql", "host")
port = ini.get_int("mysql", "port")
print("類型: ", type(host), " ", type(port))
print(host, " ", port)
# 刪除mysql中的host配置
ini.remove_option("mysql", "host")
# 刪除oracle section
ini.remove_section("oracle")
# 修改mysql port的值為4000
ini.set_option("mysql", "port", "5538")
# 最終mysql.ini中的文件內(nèi)容如下
# [mysql]
# port = 5538
# db = mysql
# user = admin
# password = 111111
items = ini.get_section_items("mysql")
print(items)
print("!!!" * 20)
運行結(jié)果如下:

總結(jié)
到此這篇關(guān)于python操作ini類型配置文件的文章就介紹到這了,更多相關(guān)python操作ini類型配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch使用過程中遇到的錯誤處理之內(nèi)存溢出問題
這篇文章主要介紹了pytorch使用過程中遇到的錯誤處理之內(nèi)存溢出問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
python文件讀寫并使用mysql批量插入示例分享(python操作mysql)
這篇文章主要介紹了python文件讀寫并使用mysql批量插入示例,可以學(xué)習(xí)到python操作mysql數(shù)據(jù)庫的方法,需要的朋友可以參考下2014-02-02
Python使用BeautifulSoup爬取網(wǎng)頁數(shù)據(jù)的操作步驟
在網(wǎng)絡(luò)時代,數(shù)據(jù)是最寶貴的資源之一,而爬蟲技術(shù)就是一種獲取數(shù)據(jù)的重要手段,Python 作為一門高效、易學(xué)、易用的編程語言,自然成為了爬蟲技術(shù)的首選語言之一,本文將介紹如何使用 BeautifulSoup 爬取網(wǎng)頁數(shù)據(jù),并提供詳細的代碼和注釋,幫助讀者快速上手2023-11-11
python使用pika庫調(diào)用rabbitmq交換機模式詳解
這篇文章主要介紹了python使用pika庫調(diào)用rabbitmq交換機模式詳解,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-08-08
python BeautifulSoup設(shè)置頁面編碼的方法
這篇文章主要介紹了python BeautifulSoup設(shè)置頁頁編碼的方法,本文直接給出代碼救命,需要的朋友可以參考下2015-04-04
詳解python和matlab的優(yōu)勢與區(qū)別
在本文中小編給大家分享的是關(guān)于python和matlab的優(yōu)勢與區(qū)別的知識點以及實例代碼,需要的朋友們參考學(xué)習(xí)下。2019-06-06

