基于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 Trie树实现字典排序
Mar 28 Python
Python聚类算法之DBSACN实例分析
Nov 20 Python
python实现中文转换url编码的方法
Jun 14 Python
python3使用flask编写注册post接口的方法
Dec 28 Python
Python面向对象程序设计示例小结
Jan 30 Python
Pandas之ReIndex重新索引的实现
Jun 25 Python
django商品分类及商品数据建模实例详解
Jan 03 Python
Python decorator拦截器代码实例解析
Apr 04 Python
python TCP包注入方式
May 05 Python
Django微信小程序后台开发教程的实现
Jun 03 Python
Python项目实战之使用Django框架实现支付宝付款功能
Feb 23 Python
实操Python爬取觅知网素材图片示例
Nov 27 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
cmd下运行php脚本
2008/11/25 PHP
php 应用程序安全防范技术研究
2009/09/25 PHP
php入门学习知识点五 关于php数组的几个基本操作
2011/07/14 PHP
PHP使用trim函数去除字符串左右空格及特殊字符实例
2016/01/07 PHP
jquery 表单进行客户端验证demo
2009/08/24 Javascript
js表格分页实现代码
2009/09/18 Javascript
使用js判断TextBox控件值改变然后出发事件
2014/03/07 Javascript
JavaScript中的比较操作符>、=、
2014/12/31 Javascript
js实现的倒计时按钮实例
2015/06/24 Javascript
深入探秘jquery瀑布流的实现
2016/01/30 Javascript
基于javascript html5实现3D翻书特效
2016/03/14 Javascript
JS实现touch 点击滑动轮播实例代码
2017/01/19 Javascript
如何在 Vue.js 中使用第三方js库
2017/04/25 Javascript
JavaScript实现的搜索及高亮显示功能示例
2017/08/14 Javascript
react-native组件中NavigatorIOS和ListView结合使用的方法
2017/09/30 Javascript
react-router browserHistory刷新页面404问题解决方法
2017/12/29 Javascript
微信小程序修改swiper默认指示器样式的实例代码
2018/07/18 Javascript
jQuery事件blur()方法的使用实例讲解
2019/03/30 jQuery
浅谈js闭包理解
2019/04/01 Javascript
Vue过渡效果之CSS过渡详解(结合transition,animation,animate.css)
2020/02/05 Javascript
python非递归全排列实现方法
2017/04/10 Python
Pycharm学习教程(1) 定制外观
2017/05/02 Python
基于python list对象中嵌套元组使用sort时的排序方法
2018/04/18 Python
Python利用pandas计算多个CSV文件数据值的实例
2018/04/19 Python
python多行字符串拼接使用小括号的方法
2020/03/19 Python
解决安装python3.7.4报错Can''t connect to HTTPS URL because the SSL module is not available
2019/07/31 Python
Python如何操作office实现自动化及win32com.client的运用
2020/04/01 Python
keras中epoch,batch,loss,val_loss用法说明
2020/07/02 Python
利用Python如何画一颗心、小人发射爱心
2021/02/21 Python
世界上最大的在线汽车租赁预订平台:Rentalcars.com(支持中文)
2018/10/12 全球购物
static关键字的用法
2013/10/07 面试题
大学毕业自我鉴定范文
2014/02/03 职场文书
领导党性分析材料
2014/02/15 职场文书
《小草和大树》教学反思
2014/02/16 职场文书
学校教师安全责任书
2014/07/23 职场文书
MySQL系列之二 多实例配置
2021/07/02 MySQL