网站首页> 后端开发> python> python操作ini类型配置文件的实例教程

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

时间:2020-10-31 08:47:00 阅读:8145次 来源:互联网

一、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类型配置文件的实例教程

本文地址:https://www.manongw.com/article/131.html

文章来源:转载于CSDN,转载网址为https://blog.csdn.net/guiyin1150/article/details/109367197

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

相关文章
  • 本文主要介绍了Jupyter Notebook安装及使用方法解析的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-11-12 17:37
  • 本文主要介绍了Python高阶函数与装饰器函数的深入讲解的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-11-10 16:18
  • 本文主要介绍了Python+OpenCV图像处理——实现轮廓发现的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-10-24 10:20
  • 本文主要介绍了python hmac模块验证客户端的合法性的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-11-08 08:38
  • 本文主要介绍了Sentry错误日志监控使用方法解析的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-11-12 16:11
  • 本文主要介绍了python 下载m3u8视频的示例代码的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-11-12 10:22
  • 本文主要介绍了Python数据可视化常用4大绘图库原理详解的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-10-24 09:27
  • 本文主要介绍了Python操作word文档插入图片和表格的实例演示的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-10-26 14:49
  • 本文主要介绍了Django执行源生mysql语句实现过程解析的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-11-13 10:34
  • 本文主要介绍了python对 MySQL 数据库进行增删改查的脚本的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
    2020-10-22 18:50