基于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中使用md5sum检查目录中相同文件代码分享
Feb 02 Python
Python中内置数据类型list,tuple,dict,set的区别和用法
Dec 14 Python
关于django 数据库迁移(migrate)应该知道的一些事
May 27 Python
python绘制直线的方法
Jun 30 Python
Django migrations 默认目录修改的方法教程
Sep 28 Python
使用Python轻松完成垃圾分类(基于图像识别)
Jul 09 Python
使用Python刷淘宝喵币(低阶入门版)
Oct 30 Python
Python面向对象之私有属性和私有方法应用案例分析
Dec 31 Python
解决安装新版PyQt5、PyQT5-tool后打不开并Designer.exe提示no Qt platform plugin的问题
Apr 24 Python
无需压缩软件,用python帮你操作压缩包
Aug 17 Python
Python本地及虚拟解释器配置过程解析
Oct 13 Python
python opencv人脸识别考勤系统的完整源码
Apr 26 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缓存技术的多种方法小结
2012/08/14 PHP
php中使用sftp教程
2015/03/30 PHP
PHP生成随机数的方法总结
2018/03/01 PHP
javascript 学习笔记(一)DOM基本操作
2011/04/08 Javascript
js中通过split函数分割字符串成数组小例子
2013/09/21 Javascript
从jquery的过滤器.filter()方法想到的
2013/09/29 Javascript
js 距离某一时间点时间是多少实现代码
2013/10/14 Javascript
jQuery实用函数用法总结
2014/08/29 Javascript
jQuery入门基础知识学习指南
2015/08/14 Javascript
详解JS面向对象编程
2016/01/24 Javascript
浅谈JQ中mouseover和mouseenter的区别
2016/09/13 Javascript
老生常谈Bootstrap媒体对象
2017/07/06 Javascript
vue 实现剪裁图片并上传服务器功能
2018/03/01 Javascript
js实现动态添加上传文件页面
2018/10/22 Javascript
JavaScript动态创建二维数组的方法示例
2019/02/01 Javascript
使用Vue父子组件通信实现todolist的功能示例代码
2019/04/11 Javascript
解决layui页面按钮点击无反应,也不报错的问题
2019/09/29 Javascript
Vue如何基于es6导入外部js文件
2020/05/15 Javascript
详解ES6中class的实现原理
2020/10/03 Javascript
python提取内容关键词的方法
2015/03/16 Python
python正则表达式的使用
2017/06/12 Python
Pycharm新手教程(只需要看这篇就够了)
2019/06/18 Python
Python 最强编辑器详细使用指南(PyCharm )
2019/09/16 Python
浅谈pytorch卷积核大小的设置对全连接神经元的影响
2020/01/10 Python
Pycharm同步远程服务器调试的方法步骤
2020/11/04 Python
英国鞋类及配饰零售商:Kurt Geiger
2017/02/04 全球购物
英国儿童图书网站:Scholastic
2017/03/26 全球购物
孤独星球出版物:Lonely Planet Publications
2018/03/17 全球购物
在weblogic中发布ejb需涉及到哪些配置文件
2012/01/17 面试题
学生手册家长评语
2014/02/10 职场文书
入党积极分子学习党的纲领思想汇报
2014/09/13 职场文书
大学生军训自我鉴定范文
2014/09/18 职场文书
家属答谢词
2015/01/05 职场文书
Vue+TypeScript中处理computed方式
2022/04/02 Vue.js
Go语言grpc和protobuf
2022/04/13 Golang
Java 定时任务技术趋势简介
2022/05/04 Java/Android