Python3 使用configparser 读写ini文件

没有标题

python中的模块还是比较优雅的,只要了解模块提供的一些方法,利用这些方法就可以完成任务了。

以下,就是一些接口,为了管理,封装了下:

import configparser

class GetINI:
	'提供读写ini类型文件和读取值得一些方法'
	def __init__(self):
		'实例化ConfigParser()对象'
		self.conf = configparser.ConfigParser()

	def Read(self, inifilename):
		'读取ini文件'
		self.conf.read(inifilename,encoding="utf-8-sig")

	#获取ini文件内所有的section,以列表形式返回['path', 'keyword']
	def GetSections(self):
		return self.conf.sections()

	#'获取指定section下所有options ,以列表形式返回['Keyword_1', 'Keyword_2']'
	def GetOptions(self,section):	
		return self.conf.options(section)

	#'获取指定section下所有的键值对,以元祖列表形式返回[('Keyword_1', 'F_PAN'), ('Keyword_2', 'F_VALIDDATE')]'
	def GetItems(self,section):	
		return self.conf.items(section)

	#'获取指定section中option的值,返回为string类型'
	def Get(self,section,option):
		return self.conf.get(section,option)

	#判断是否存在指定section
	def HasSection(self,section):
		return self.conf.has_section(section)


	#删除指定section
	def DelSection(self,section):
		self.conf.remove_section(section)

	#增加指定section
	def AddSection(self,section):
		self.conf.add_section(section)

	#增加指定section下的key和value
	def AddOption(self,section,key,value):
		self.conf.set(section,key,value)

	#写指令,对配置文件进行修改后,一定要执行写指令
	def Write(self,inifilename):
		with open(inifilename,'w') as configfile:
			self.conf.write(configfile)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值