python操作ini类型配置文件的实例教程
#代码知识 发布时间: 2026-01-12
一、ini文件介绍

INI文件格式是某些平台或软件上的配置文件的非正式标准,以节(section)和键(key)构成,常用于微软Windows操作系统中。这种配置文件的文件扩展名多为INI
二、ini文件的结构
- 片段[section]
- 键名 option
- 值 value
三、实例:
实例1
python25.ini
[teachers]
name = ['yushen', 'pianpian']
age = 16
gender = '女'
favor = {"movie": "追风", "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))
运行结果如下:
实例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)
# 创建管理对象
conf = configparser.ConfigParser()
# 读ini文件
conf.read(cfgpath, encoding="utf-8")
# 获取所有的section
sections = conf.sections()
# 返回list
print(sections)
items = conf.items('oracle')
# list里面对象是元祖
print(items)
运行结果:
实例3,封装升级
set修改,add添加,write写入、remove删除
此封装实现以下功能:
- 获取sections列表
- 获取指定的section的options列表
- 获取指定section的配置信息列表
- 按类型读取配置信息
- 新增section
- 设置指定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"))
# 设置指定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标准库解析实例======根据需求运行代码!!!")
# 如果存在mysql.ini先删除,方便下列代码的运行
if os.path.exists("mysql.ini"):
os.remove("mysql.ini")
# 我们先写一些数据到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中的文件内容如下
# [mysql]
# port = 5538
# db = mysql
# user = admin
# password = 111111
items = ini.get_section_items("mysql")
print(items)
print("!!!" * 20)
运行结果如下:
总结
代码知识SEO上一篇 : Redis概述及linux安装redis的详细教程
下一篇 : Android选中突出背景效果的底部导航栏功能
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!