python自带的ide的使用(基于python的ini配置文件操作工具类)
类别:脚本大全 浏览量:927
时间:2021-10-13 00:52:27 python自带的ide的使用
基于python的ini配置文件操作工具类本文实例为大家分享了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 |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/carolcoral/article/details/80841869
您可能感兴趣
- python和mysql实战(由Python编写的MySQL管理工具代码实例)
- python弹球游戏编写过程(python实现坦克大战游戏 附详细注释)
- python如何编写判断正负数程序(Python实现判断一个整数是否为回文数算法示例)
- python代码如何进行切片索引(Python读取Pickle文件信息并计算与当前时间间隔的方法分析)
- python 写入d盘文件(python文件写入write的操作)
- python2个字典合成(Python两个字典键同值相加的几种方法)
- python模块使用方法(详解python的argpare和click模块小结)
- python实现的数据结构(Python嵌套式数据结构实例浅析)
- python里range循环(Python for循环与range函数的使用详解)
- python 多进程的启动和停止(Python3.5多进程原理与用法实例分析)
- python3循环使用教程(Python3.4学习笔记之 idle 清屏扩展插件用法分析)
- python time模块是什么(python中时间模块的基本使用教程)
- python自带的ide的使用(基于python的ini配置文件操作工具类)
- 随意化快排python算法(python快排算法详解)
- python协程结果(深入浅析python 协程与go协程的区别)
- python类中的数据封装(基于python生成器封装的协程类)
- 新一小兰领衔 盘点动漫中的那些 远距离恋爱情侣(盘点动漫中的那些)
- 大事件 合肥四中火了(大事件合肥四中火了)
- 翼龙贷组织出借人调研 感受鄱阳 借 来的致富路(翼龙贷组织出借人调研)
- 2023新国风戏曲教育寒假集训班汇报演出《戏娃闹元宵》图文报道(2023新国风戏曲教育寒假集训班汇报演出戏娃闹元宵图文报道)
- 九儿《狐踪谍影》出演热血女特警,戏份杀青受关注(九儿狐踪谍影出演热血女特警)
- 红色代表什么(红色代表什么寓意)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9