文章详情

一、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))

运行结果如下:

python操作ini类型配置文件的实例教程

实例2

fz.ini

python操作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)

运行结果:

python操作ini类型配置文件的实例教程

实例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)

运行结果如下:

python操作ini类型配置文件的实例教程

您可能感兴趣的文章:

  • 如何在 Python 中使用 try…else 块
  • 关于Python的异常捕获和处理
  • 关于使用Python的time库制作进度条程序
  • 关于python的第三方库下载与更改方式
  • Python的turtle绘图库使用基础
  • python如何用正则表达式提取字符串
  • python中的txt文件转换为XML
  • 详解Python中四种关系图数据可视化的效果对比

版权:版权申明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 70068002@qq.com 举报,一经查实,本站将立刻删除。

转载请注明出处:https://www.stntk.com/2615.html/dongtai/houduan/

相关推荐
php数据库怎样去重复的数据
当涉及到数据库去重功能时,PHP可以与数据库交互并执行相应的操作。以下是一个示例的PHP代码,用于从数据库中删除重复的记录并保留唯一的数据:…
头像
后端开发 2024-05-26
1,069
如何在 Python 中使用 try…else 块
在 Python 编程中,我们经常会遇到需要处理异常的情况。使用 try…except 块是一种常见的处理方法,它可以捕获和处理代码中可能出…
头像
后端开发 2024-05-26
1,353
php发送/显示 base64 编码图像
我需要向客户端发送一个 base64 编码的字符串。因此,我打开并读取服务器上的图像文件,对其进行编码并将该数据与 image/jpeg 内…
头像
后端开发 2024-05-26
11,560
php 生成条形码(支持任意php框架)
一:插件安装 在php中我们可以使用php-barcode-generator插件来生成条形码,php-barcode-generator插…
头像
后端开发 2024-05-26
1,020
如何在ThinkPHP6中使用队列技术
ThinkPHP 6(TP6)是一个流行的PHP框架,它提供了一些有用的工具和组件,其中之一就是队列(Queue)服务。队列是一种用于异步处…
头像
后端开发 2024-05-26
1,600
cURL error 60: SSL certificate problem: unable to get local issuer certificate 解决方法
fastadmin开发小程序登录功能报错: cURL error 60: SSL certificate problem: unable t…
头像
后端开发 2024-05-26
1,117
发表评论
暂无评论

还没有评论呢,快来抢沙发~

点击联系客服

在线时间:8:00-16:00

客服电话

400-888-8888

客服邮箱

70068002@qq.com

扫描二维码

关注微信公众号