基于python的ini配置文件操作工具类


Posted in Python onApril 24, 2019

本文实例为大家分享了python的ini配置文件操作工具类的具体代码,供大家参考,具体内容如下

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
  @Time  : 2018/6/22
  @Author : LiuXueWen
  @Site  : 
  @File  : Util_Ini_Operation.py
  @Software: PyCharm
  @Description: ini配置文件操作工具类
    1.读取.ini配置文件
    2.修改.ini配置文件
    [section]
    option:value
"""
import ConfigParser

'''
  基础读取配置文件
    -read(filename)     直接读取文件内容
    -sections()       得到所有的section,并以列表的形式返回
    -options(section)    得到该section的所有option
    -items(section)     得到该section的所有键值对
    -get(section,option)  得到section中option的值,返回为string类型
    -getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
'''
class get_ini():

  # 初始化配置文件对象
  def __init__(self,path):
    # 实例化
    self.cf = ConfigParser.ConfigParser()
    # 读取配置文件
    self.cf.read(path)

  # 获取所有的sections
  def get_sections(self):
    sections = self.cf.sections()
    return sections

  # 获取section下的所有key
  def get_options(self,section):
    opts = self.cf.options(section=section)
    return opts

  # 获取section下的所有键值对
  def get_kvs(self,section):
    kvs = self.cf.items(section=section)
    return kvs

  # 根据section和option获取指定的value
  def get_key_value(self,section,option):
    opt_val = self.cf.get(section=section,option=option)
    return opt_val

  # 更新指定section的option下的value
  # def update_section_option_val(self,section,option,value,path,module):
  #   self.cf.set(section=section,option=option,value=value)
  #   with open(path,module) as f:
  #     self.cf.write(f)

'''
  基础写入配置文件
    -write(fp)             将config对象写入至某个 .init 格式的文件 Write an .ini-format representation of the configuration state.
    -add_section(section)       添加一个新的section
    -set(section, option, value)    对section中的option进行设置,需要调用write将内容写入配置文件 ConfigParser2
    -remove_section(section)      删除某个 section
    -remove_option(section, option)  删除某个 section 下的 option
'''
class write_ini():

  def __init__(self,path,module):
    # 实例化配置对象
    self.cf = ConfigParser.ConfigParser()
    # 获取写入文件路径,若采用w+方式则该文件可以不存在
    self.path = path
    # 配置写入方式,写入方式"w+"清空写
    self.module = module

  # 写入配置文件
  def write_ini_file(self):
    with open(self.path,self.module) as f:
      self.cf.write(f)

  # 新增section
  def add_section(self,section):
    self.cf.add_section(section=section)
    self.write_ini_file()

  # 删除某个 section
  def remove_section(self,section):
    self.cf.remove_section(section=section)
    self.write_ini_file()

  # 删除某个 section 下的 option
  def remove_option(self,section,option):
    self.cf.remove_option(section=section,option=option)
    self.write_ini_file()

if __name__ == '__main__':
  pass

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python引用(import)文件夹下的py文件的方法
Aug 26 Python
Python Tkinter简单布局实例教程
Sep 03 Python
在Python中利用Pandas库处理大数据的简单介绍
Apr 07 Python
Python编程之基于概率论的分类方法:朴素贝叶斯
Nov 11 Python
python3获取两个日期之间所有日期,以及比较大小的实例
Apr 08 Python
python自动查询12306余票并发送邮箱提醒脚本
May 21 Python
实例讲解Python爬取网页数据
Jul 08 Python
Pycharm之快速定位到某行快捷键的方法
Jan 20 Python
python编程进阶之类和对象用法实例分析
Feb 21 Python
python中可以声明变量类型吗
Jun 18 Python
详解torch.Tensor的4种乘法
Sep 03 Python
python判断字符串以什么结尾的实例方法
Sep 18 Python
python实现简单日期工具类
Apr 24 #Python
NumPy 基本切片和索引的具体使用方法
Apr 24 #Python
Python使用dict.fromkeys()快速生成一个字典示例
Apr 24 #Python
python3中property使用方法详解
Apr 23 #Python
详解爬虫被封的问题
Apr 23 #Python
Python3.5 Pandas模块缺失值处理和层次索引实例详解
Apr 23 #Python
Python3.5 Pandas模块之DataFrame用法实例分析
Apr 23 #Python
You might like
php访问查询mysql数据的三种方法
2006/10/09 PHP
用PHPdig打造属于你自己的Google[图文教程]
2007/02/14 PHP
php正则表达式获取内容所有链接
2015/07/24 PHP
PHP正则表达式处理函数(PCRE 函数)实例小结
2019/05/09 PHP
用js自动判断浏览器分辨率的代码
2007/01/28 Javascript
CSS鼠标响应事件经过、移动、点击示例介绍
2013/09/04 Javascript
nodejs开发环境配置与使用
2014/11/17 NodeJs
浅谈JavaScript 覆盖原型以及更改原型
2016/08/31 Javascript
Node.js websocket使用socket.io库实现实时聊天室
2017/02/20 Javascript
整理关于Bootstrap列表组的慕课笔记
2017/03/29 Javascript
微信小程序实战之仿android fragment可滑动底部导航栏(4)
2020/04/16 Javascript
Bootstrap与Angularjs的模态框实例代码
2017/08/03 Javascript
详谈JS中数组的迭代方法和归并方法
2017/08/11 Javascript
webstorm中配置nodejs环境及npm的实例
2018/05/15 NodeJs
JavaScript键盘事件响应顺序详解
2019/09/30 Javascript
Python 解析XML文件
2009/04/15 Python
Python装饰器使用示例及实际应用例子
2015/03/06 Python
Python中使用copy模块实现列表(list)拷贝
2015/04/14 Python
Python简单实现查找一个字符串中最长不重复子串的方法
2018/03/26 Python
python pandas 组内排序、单组排序、标号的实例
2018/04/12 Python
Python Matplotlib库安装与基本作图示例
2019/01/09 Python
python判断字符串或者集合是否为空的实例
2019/01/23 Python
基于Tensorflow:CPU性能分析
2020/02/10 Python
使用python将微信image下.dat文件解密为.png的方法
2020/11/30 Python
HTML5 Canvas中绘制椭圆的4种方法
2015/04/24 HTML / CSS
Bath & Body Works阿联酋:在线购买沐浴和身体用品
2021/02/27 全球购物
高中军训广播稿
2014/01/14 职场文书
幼儿园教师奖惩制度
2014/02/01 职场文书
旅游管理毕业生自荐信范文
2014/03/19 职场文书
效能监察建议书
2014/05/19 职场文书
演讲稿格式范文
2014/05/19 职场文书
爱的奉献演讲稿
2014/09/10 职场文书
2014年文明创建工作总结
2014/11/25 职场文书
法务专员岗位职责
2015/02/14 职场文书
资料员岗位职责范本
2015/04/13 职场文书
幼儿园庆六一主持词
2015/06/30 职场文书