配置檔案的操作和封裝

2021-10-08 20:04:29 字數 1543 閱讀 4478

常用的配置檔案格式.ini/.conf

配置檔案的建立(conf.ini)

[logging]

level = debug

f_level = debug

s_level = error

[desire]

name = desire

age = 18

*** = 男

money = 1000.99

switch = true

配置檔案的操作

1、導包

from configparser import configparser
2、建立乙個操作配置檔案的物件(檔案解析物件)
conf = configparser()
3、讀取配置檔案中的內容
conf.read("conf.ini",encoding="utf8")
get方法:讀取出來的內容都是字串
res1 = conf.get("logging", "level")
getint:讀取整數型別的資料,讀取出來是int型別
res3 = conf.getint("desire","age")
getfloat:讀取浮點數
res4 = conf.getfloat("desire", "money")
getboolean:讀取布林值
res5 = conf.getboolean("desire", "switch")
4、配置資料的寫入
conf.set("desire", "bbb", "python999")

conf.write(open("conf.ini", "w",encoding="utf-8"))

class myconf(configparser):

"""封裝的讀取配置檔案類"""

def __init__(self, file_name, encoding='utf8'):

"""初始化

:param file_name: 配置檔名

:param encoding: 編碼格式

"""super().__init__()

self.file_name = file_name

self.encoding = encoding

self.read(file_name, encoding=encoding)

def write_data(self, section, option, value):

"""新增值

:param section: 配置塊

:param option: 配置屬性

:param value: 對應的配置屬性值

"""self.set(section, option, value)

self.write(open(self.file_name, 'w', encoding=self.encoding))

MFC 讀取配置檔案(封裝)

目前只封裝了string型別的讀和寫配置檔案 利用了 windows api 現有的函式,很簡單 writeprivateprofilestringw in opt lpcwstr lpkeyname,in opt lpcwstr lpstring,in opt lpcwstr lpfilename...

配置檔案INI的操作

這個段 lpkeyname包含了乙個鍵的名字,沒有該鍵則建立,如果該引數為null,則整 個段,包括段中所有的項都將被刪除 lpstring是被寫入win.ini檔案的字串,如果 lpkeyname,lpctstr lpdefault,lptstr lpreturnedstring,dword ns...

配置檔案的讀寫操作

配置檔案用來記錄登入使用者的設定資訊,使得在下一次登陸時能夠從配置檔案中讀出相應使用者的設定資訊,實現的具體 如下 1 先定義個乙個結構體,使用者所有的資訊字段 typedef struct personinfo personinfo const personinfo stpersoninfo 2 ...