python讀取ini配置的類封裝

2021-10-02 01:35:42 字數 3126 閱讀 9939

# coding:utf-8

import configparser

import os

class inicfg():

def __init__(self):

self.conf = configparser.configparser()

self.cfgpath = ''

def checksection(self, section):

try:

self.conf.items(section)

except exception:

print(">> 無此section,請核對[%s]" % section)

return none

return true

# 讀取ini,並獲取所有的section名

def readsectionitems(self, cfgpath):

if not os.path.isfile(cfgpath):

print(">> 無此檔案,請核對路徑[%s]" % cfgpath)

return none

self.cfgpath = cfgpath

self.conf.read(cfgpath, encoding="utf-8")

return self.conf.sections()

# 讀取乙個section,list裡面物件是元祖

def readonesection(self, section):

try:

item = self.conf.items(section)

except exception:

print(">> 無此section,請核對[%s]" % section)

return none

return item

# 讀取乙個section到字典中

def prettysectodic(self, section):

if not self.checksection(section):

return none

res = {}

for key, val in self.conf.items(section):

res[key] = val

return res

# 讀取所有section到字典中

def prettysecstodic(self):

res_1 = {}

res_2 = {}

sections = self.conf.sections()

for sec in sections:

for key, val in self.conf.items(sec):

res_2[key] = val

res_1[sec] = res_2.copy()

res_2.clear()

return res_1

# 刪除乙個 section中的乙個item(以鍵值key為標識)

def removeitem(self, section, key):

if not self.checksection(section):

return

self.conf.remove_option(section, key)

# 刪除整個section這一項

def removesection(self, section):

if not self.checksection(section):

return

self.conf.remove_section(section)

# 新增乙個section

def addsection(self, section):

self.conf.add_section(section)

# 往section新增key和value

def additem(self, section, key, value):

if not self.checksection(section):

return

self.conf.set(section, key, value)

# 執行write寫入, remove和set方法並沒有真正的修改ini檔案內容,只有當執行conf.write()方法的時候,才會修改ini檔案內容

def actionoperate(self, mode):

if mode == 'r+':

conf.write(open(self.cfgpath, "r+", encoding="utf-8")) # 修改模式

elif mode == 'w':

conf.write(open(self.cfgpath, "w")) # 刪除原檔案重新寫入

elif mode == 'a':

conf.write(open(self.cfgpath, "a")) # 追加模式寫入

cfgpath = r'c:\users\sxf\desktop\config.ini'

inicfg = inicfg()

sections = inicfg.readsectionitems(cfgpath)

print(sections)

content = inicfg.readonesection('chaoji')

print(content)

dic = inicfg.prettysectodic('chaoji')

print(dic)

dic = inicfg.prettysecstodic()

print(dic)

inicfg.addsection('chaoji22')

content = inicfg.readonesection('chaoji')

print(content)

[chaoji]

chaoji_username = 123

chaoji_password = 456

[my]

soft_id = 789

sleeptime = asd

cnt_count = zxc

python 讀取ini配置檔案

使用配置檔案裝載一些條件,以及一些對資料庫進行操作的語句等 作為對資料處理的一些外接條件。import configparser config file r format interdoc conf config configparser.configparser config.read confi...

python中配置ini檔案讀取

今天分享下關於python讀取配置檔案的相關知識點,這裡主要介紹python的configparser模組的用法。話不多說,下面直接上 講解。首先我們要知道配置檔案中的格式要求是什麼?說起來也很簡單,直接看看下面的示例吧 group1 name name1 age 20 tel 1385 698 a...

python讀取 ini 配置檔案

在詳解python讀取ini檔案之前,我們先說明乙個ini檔案的組成 乙個ini檔案是由多個section組成,每個section中以key vlaue形式儲存資料 然後我們來使用python讀取ini檔案中的資料 1導包 導包import configparser config configpar...