基于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

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

(0)

相关推荐

  • Python操作配置文件ini的三种方法讲解

    python 操作配置文件ini的三种方法 方法一:crudini 命令 说明 crudini命令是Linux下的一个操作配置文件的命令工具 用法 crudini --set [--existing] config_file section [param] [value] # 修改配置文件内容 crudini --get [--format=sh|ini] config_file [section] [param] # 获取配置文件内容 crudini --del [--existing] co

  • Python使用自带的ConfigParser模块读写ini配置文件

    在用Python做开发的时候经常会用到数据库或者其他需要动态配置的东西,硬编码在里面每次去改会很麻烦.Python自带有读取配置文件的模块ConfigParser,使用起来非常方便. ini文件 ini配置文件格式: 读取配置文件: import ConfigParser conf = ConfigParser.ConfigParser() conf.read('dbconf.ini') # 文件路径 name = conf.get("section1", "name&quo

  • Python中使用ConfigParser解析ini配置文件实例

    ini文件是windows中经常使用的配置文件,主要的格式为: 复制代码 代码如下: [Section1] option1 : value1 option2 : value2 python提供了一个简单的模块ConfigParser可以用来解析类似这种形式的文件.对于ConfigParser模块可以解析key:value和key=value这样的类型,对于#和;开头的行将会自动忽视掉.相当于注释行.常用的函数: 复制代码 代码如下: ConfigParser.RawConfigParser()

  • Python实现读写INI配置文件的方法示例

    本文实例讲述了Python实现读写INI配置文件的方法.分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- import ConfigParser import os '''读写配置文件的类 [section] logpath = D:\log\ imageminsize = 200 ''' class ConfigFile: '''构造函数:初始化''' def __init__(self,fileName): fileName = unicode(fileNam

  • python对配置文件.ini进行增删改查操作的方法示例

    前言 本文主要给大家介绍的是关于python对配置文件.ini增删改查操作的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 一.先导入configobj库文件 可以用pip直接安装 #!/usr/bin/python # -*- coding: utf-8 -*- import json from configobj import ConfigObj 二.增添section 这里是前后端分离的例子,从前端接收json数据,然后写入配置文件中 def add(self,

  • python读写ini配置文件方法实例分析

    本文实例讲述了python读写ini配置文件方法.分享给大家供大家参考.具体实现方法如下: import ConfigParser import os class ReadWriteConfFile: currentDir=os.path.dirname(__file__) filepath=currentDir+os.path.sep+"inetMsgConfigure.ini" @staticmethod def getConfigParser(): cf=ConfigParser

  • 基于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.修改.in

  • C++读写INI配置文件的类实例

    本文实例讲述了C++读写INI配置文件的类.分享给大家供大家参考.具体如下: 1. IniReader.h文件: #ifndef INIREADER_H #define INIREADER_H #include <windows.h> class CIniReader { public: CIniReader(LPCTSTR szFileName); int ReadInteger(LPCTSTR szSection, LPCTSTR szKey, int iDefaultValue); fl

  • python读取ini配置文件过程示范

    这篇文章主要介绍了python读取ini配置文件过程示范,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装 pip install configparser 1 配置文件 config.ini: [MysqlDB] user=root passwd=123456 sport=3306 db_name=my_db charset=utf-8 获取参数: import configparser config = configparser.Conf

  • python读取ini配置的类封装代码实例

    这篇文章主要介绍了python读取ini配置的类封装代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 此为基础封装,未考虑过多异常处理 类 # coding:utf-8 import configparser import os class IniCfg(): def __init__(self): self.conf = configparser.ConfigParser() self.cfgpath = '' def checkSec

  • Python读取ini配置文件传参的简单示例

    前言 为了往我们写好的Python代码传入参数,有很多种方法,比如使用input获取从DOS 输入的参数,又或者读取txt 文件中的字符作为参数.但为了比较规范,在windows 上我们常常用ini的配置文件进行工具配置.因此,今天我们说明下如果使用python 读取ini 文件. 一.后缀 ini 配置文件介绍 我们新建一个txt 文件,将后缀改为.ini形式,在ini文件中按照分组写入需要的参数. ini示例: # 定义arnold分组 [arnold] # 分组名称 platformNam

  • Python中ini配置文件读写的实现

    导入模块 import configparser # py3 写入 config = configparser.ConfigParser() config["DEFAULT"] = {     'ServerAliveInterval': '45',     'Compression': 'yes',     'CompressionLevel': '9'     } config['bitbucket.org'] = {} config['bitbucket.org']['User'

  • Python的ini配置文件你了解吗

    目录 INI介绍 关于configparser INI文件格式 读取配置文件 总结 INI介绍 INI是英文“初始化”(initialization)的缩写,被用来对操作系统或特定程序初始化或进行参数设置.由节(section). 键(key).值(value)构成.在windows系统中有很多INI文件,例如“System32.ini”和“Win.ini”,相信大家并不陌生.Python 中操作配置文件的模块为configparser,这个模块可以用来解析与Windows上INI文件结构类似的

  • 基于Python3读写INI配置文件过程解析

    ini文件简介 ini是我们常见到的配置文件格式之一. ini是微软Windows操作系统中的文件扩展名(也常用在其他系统). INI是英文"初始化(Initial)"的缩写.正如该术语所表示的,INI文件被用来对操作系统或特定程序初始化或进行参数设置. 通过它,可以将经常需要改变的参数保存起来(而且还可读),使程序更加的灵活. 我先给出一个ini文件的示例. [School] ip = 10.15.40.123 mask = 255.255.255.0 gateway = 10.15

  • Python实现解析ini配置文件的示例详解

    目录 楔子 ini 文件 特殊格式 小结 楔子 在开发过程中,配置文件是少不了的,只不过我们有时会将 py 文件作为配置文件(config.py),然后在其它的模块中直接导入.这样做是一个好主意,不过配置文件是有专门的格式的,比如:ini, yaml, toml 等等. 而对于 Python 而言,也都有相应的库来解析相应格式的文件,下面我们来看看 ini 文件要如何解析. ini 文件 先来了解一下 ini 文件的格式: [satori] name = 古明地觉 age = 16 where 

随机推荐