python 读取ini配置文件模块 configobj 介绍
2012-12-05 15:40
691 查看
下面是我写的关于这个模块使用的一些代码:(仅供参考)
# -*- coding:utf-8 -*-
import string
from configobj import ConfigObj
class IniFileOperator:
def __init__(self):
self.inifile = "config.ini"
#获取ini参数值
def get_ini_val(self,inifile,section,option):
config = ConfigObj(inifile)
val=config[section][option]
if val!=None and val!="":
return val
else:
return False
#获取ini键值
def get_ini_option(self,inifile,section):
config = ConfigObj(inifile)
sect = config.keys()
if sect.count(section) :
option = config[section]
return option.keys()
else :
return ""
#获取ini选项
def get_ini_section(self,inifile):
config = ConfigObj(inifile)
section = config.keys()
#print 'sections:', section
return section
#修改ini段值
def set_ini_section(self,inifile,section):
config = ConfigObj(inifile)
config[section] = {}
print "set ini_section is: ",section
config.write()
#修改ini键值
def set_ini_val(self,inifile,section,option,val):
config = ConfigObj(inifile)
sect = config.keys()
if sect.count(section) :
config[section][option] = val
print "set ini_option is: %s = %s" %(option,val)
else :
config[section] = {}
config[section][option] = val
print "set ini_option is: %s = %s" %(option,val)
config.write()
#删除ini键值
def del_ini_option(self,inifile,section,option):
config = ConfigObj(inifile)
sect = config[section]
if sect.keys().count(option) :
del config[section][option]
print "delete ini_option is: %s" %option
config.write()
更详细的介绍或者configobj下载地址 :http://www.voidspace.org.uk/python/configobj.html
# -*- coding:utf-8 -*-
import string
from configobj import ConfigObj
class IniFileOperator:
def __init__(self):
self.inifile = "config.ini"
#获取ini参数值
def get_ini_val(self,inifile,section,option):
config = ConfigObj(inifile)
val=config[section][option]
if val!=None and val!="":
return val
else:
return False
#获取ini键值
def get_ini_option(self,inifile,section):
config = ConfigObj(inifile)
sect = config.keys()
if sect.count(section) :
option = config[section]
return option.keys()
else :
return ""
#获取ini选项
def get_ini_section(self,inifile):
config = ConfigObj(inifile)
section = config.keys()
#print 'sections:', section
return section
#修改ini段值
def set_ini_section(self,inifile,section):
config = ConfigObj(inifile)
config[section] = {}
print "set ini_section is: ",section
config.write()
#修改ini键值
def set_ini_val(self,inifile,section,option,val):
config = ConfigObj(inifile)
sect = config.keys()
if sect.count(section) :
config[section][option] = val
print "set ini_option is: %s = %s" %(option,val)
else :
config[section] = {}
config[section][option] = val
print "set ini_option is: %s = %s" %(option,val)
config.write()
#删除ini键值
def del_ini_option(self,inifile,section,option):
config = ConfigObj(inifile)
sect = config[section]
if sect.keys().count(option) :
del config[section][option]
print "delete ini_option is: %s" %option
config.write()
更详细的介绍或者configobj下载地址 :http://www.voidspace.org.uk/python/configobj.html
相关文章推荐
- Python中INI配置文件的读取
- Python读取修改ini配置文件[ConfigParser]
- Python + Selenium自动化测试 -- 读取ini配置文件
- 通过python读取ini配置文件
- python读取配置文件ini
- python-配置解析模块configparser-读取配置文件-遍历配置文件
- 通过python的ConfigParse模块读写ini配置文件
- Python读取ini配置文件
- Python模块之: ConfigParser 配置文件读取
- 通过python读取ini配置文件
- python中利用ConfigParser模块读取配置文件
- Python学习之读取配置文件模块ConfigParser详解
- 详解Python读取配置文件模块ConfigParser
- 通过python读取ini配置文件
- 使用python的ConfigParser模块方便的读取配置文件
- Python recipe(11):读取ini配置文件
- Python使用自带的ConfigParser模块读写ini配置文件
- Python使用自带的ConfigParser模块读写ini配置文件
- 如何使用Python3读取配置文件(ini)
- python读取ini配置文件