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


Posted in Python onOctober 30, 2020

一、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操作ini类型配置文件的文章就介绍到这了,更多相关python操作ini类型配置文件内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
全面了解python字符串和字典
Jul 07 Python
selenium+python实现自动登录脚本
Apr 22 Python
python批量修改文件编码格式的方法
May 31 Python
pandas值替换方法
Jul 10 Python
Sanic框架Cookies操作示例
Jul 17 Python
Python操作rabbitMQ的示例代码
Mar 19 Python
PyQt5 多窗口连接实例
Jun 19 Python
python实现中文文本分句的例子
Jul 15 Python
在PyCharm的 Terminal(终端)切换Python版本的方法
Aug 02 Python
python logging 重复写日志问题解决办法详解
Aug 04 Python
python中最小二乘法详细讲解
Feb 19 Python
Python if else条件语句形式详解
Mar 24 Python
4款Python 类型检查工具,你选择哪个呢?
Oct 30 #Python
python从PDF中提取数据的示例
Oct 30 #Python
详解python百行有效代码实现汉诺塔小游戏(简约版)
Oct 30 #Python
python boto和boto3操作bucket的示例
Oct 30 #Python
python 多进程和协程配合使用写入数据
Oct 30 #Python
python打包生成so文件的实现
Oct 30 #Python
pytorch 移动端部署之helloworld的使用
Oct 30 #Python
You might like
MySql中正则表达式的使用方法描述
2008/07/30 PHP
PHP similar_text 字符串的相似性比较函数
2010/05/26 PHP
PHP数据库操作之基于Mysqli的数据库操作类库
2014/04/19 PHP
PHP实现mysqli批量执行多条语句的方法示例
2017/07/22 PHP
thinkPHP实现基于ajax的评论回复功能
2018/06/22 PHP
php接口隔离原则实例分析
2019/11/11 PHP
JS小游戏之宇宙战机源码详解
2014/09/25 Javascript
用js编写的简单的计算器代码程序
2015/08/04 Javascript
jQuery事件绑定用法详解(附bind和live的区别)
2016/01/19 Javascript
JS排序方法(sort,bubble,select,insert)代码汇总
2016/01/30 Javascript
jQuery Ajax传值到Servlet出现乱码问题的解决方法
2016/10/09 Javascript
JS控制页面跳转时未请求要跳转的地址怎么回事
2016/10/14 Javascript
利用纯Vue.js构建Bootstrap组件
2016/11/03 Javascript
JavaScript自定义浏览器滚动条兼容IE、 火狐和chrome
2017/01/05 Javascript
JS基于正则截取替换特定字符之间字符串操作示例
2017/02/03 Javascript
判断jQuery是否加载完成,没完成继续判断的解决方法
2017/12/06 jQuery
Vue中CSS动画原理的实现
2019/02/13 Javascript
react同构实践之实现自己的同构模板
2019/03/13 Javascript
微信小程序实现手指拖动选项排序
2020/04/22 Javascript
Element-ui el-tree新增和删除节点后如何刷新tree的实例
2020/08/31 Javascript
[06:30]DOTA2英雄梦之声_第15期_死亡先知
2014/06/21 DOTA
解决Django模板无法使用perms变量问题的方法
2017/09/10 Python
Python创建对称矩阵的方法示例【基于numpy模块】
2017/10/12 Python
python多进程下实现日志记录按时间分割
2019/07/22 Python
基于python操作ES实例详解
2019/11/16 Python
python3实现elasticsearch批量更新数据
2019/12/03 Python
基于python计算并显示日间、星期客流高峰
2020/05/07 Python
Python替换NumPy数组中大于某个值的所有元素实例
2020/06/08 Python
python调用有道智云API实现文件批量翻译
2020/10/10 Python
css3学习之2D转换功能详解
2016/12/23 HTML / CSS
台湾百利市购物中心:e-Payless
2017/08/16 全球购物
英国著名药妆店:Superdrug
2021/02/13 全球购物
力学专业毕业生自荐信
2013/11/17 职场文书
秋季运动会通讯稿
2014/01/24 职场文书
2014年班级工作总结范文
2014/12/23 职场文书
Hive常用日期格式转换语法
2022/06/25 数据库