基于python的ini配置文件操作工具類
更新時(shí)間:2019年04月24日 09:30:50 作者:CarolLXW
這篇文章主要為大家詳細(xì)介紹了基于python的ini配置文件操作工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python的ini配置文件操作工具類的具體代碼,供大家參考,具體內(nèi)容如下
#!/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
'''
基礎(chǔ)讀取配置文件
-read(filename) 直接讀取文件內(nèi)容
-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到該section的所有option
-items(section) 得到該section的所有鍵值對(duì)
-get(section,option) 得到section中option的值,返回為string類型
-getint(section,option) 得到section中option的值,返回為int類型,還有相應(yīng)的getboolean()和getfloat() 函數(shù)。
'''
class get_ini():
# 初始化配置文件對(duì)象
def __init__(self,path):
# 實(shí)例化
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下的所有鍵值對(duì)
def get_kvs(self,section):
kvs = self.cf.items(section=section)
return kvs
# 根據(jù)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)
'''
基礎(chǔ)寫入配置文件
-write(fp) 將config對(duì)象寫入至某個(gè) .init 格式的文件 Write an .ini-format representation of the configuration state.
-add_section(section) 添加一個(gè)新的section
-set(section, option, value) 對(duì)section中的option進(jìn)行設(shè)置,需要調(diào)用write將內(nèi)容寫入配置文件 ConfigParser2
-remove_section(section) 刪除某個(gè) section
-remove_option(section, option) 刪除某個(gè) section 下的 option
'''
class write_ini():
def __init__(self,path,module):
# 實(shí)例化配置對(duì)象
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()
# 刪除某個(gè) section
def remove_section(self,section):
self.cf.remove_section(section=section)
self.write_ini_file()
# 刪除某個(gè) section 下的 option
def remove_option(self,section,option):
self.cf.remove_option(section=section,option=option)
self.write_ini_file()
if __name__ == '__main__':
pass
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python下如何讓web元素的生成更簡(jiǎn)單的分析
做web不簡(jiǎn)單,特別是當(dāng)你需要使用一些web效果的時(shí)候, 比如顯示個(gè)圓角矩形,提示框之類的,也許你認(rèn)為很簡(jiǎn)單,好讓我們分析一下:2008-07-07
Python原始字符串與Unicode字符串操作符用法實(shí)例分析
這篇文章主要介紹了Python原始字符串與Unicode字符串操作符用法,結(jié)合實(shí)例形式分析了Python針對(duì)原始字符與Unicode字符的操作符用法,需要的朋友可以參考下2017-07-07
python操作注冊(cè)表的方法實(shí)現(xiàn)
Python提供了winreg模塊,可以用于操作Windows注冊(cè)表,本文就來介紹一下python操作注冊(cè)表的方法實(shí)現(xiàn),主要包括打開注冊(cè)表、讀取注冊(cè)表值、寫入注冊(cè)表值和關(guān)閉注冊(cè)表,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
python使用openpyxl打開及讀取excel表格過程
openpyxl是一個(gè)Python庫(kù),用于讀寫Excel?2010?xlsx/xlsm文件,它允許你輕松工作與Excel表格,進(jìn)行數(shù)據(jù)處理和分析,支持讀取、創(chuàng)建和修改Excel文件,甚至可以在Excel中插入圖表等,安裝非常簡(jiǎn)單,只需要使用pip命令即可2024-09-09
python統(tǒng)計(jì)列表中元素出現(xiàn)次數(shù)的三種方法
這篇文章主要介紹了python統(tǒng)計(jì)列表中元素出現(xiàn)次數(shù)的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08

