#!/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
''' 基礎讀取配置檔案
-read(filename) 直接讀取檔案內容
-sections() 得到所有的section,並以列表的形式返回
-options(section) 得到該section的所有option
-items(section) 得到該section的所有鍵值對
-get(section,option) 得到section中option的值,返回為string型別
-getint(section,option) 得到section中option的值,返回為int型別,還有相應的getboolean()和getfloat() 函式。
'''class
get_ini()
:# 初始化配置檔案物件
def__init__
(self,path)
:# 例項化
self.cf = configparser.configparser(
)# 讀取配置檔案
self.cf.read(path)
# 獲取所有的sections
defget_sections
(self)
: sections = self.cf.sections(
)return sections
# 獲取section下的所有key
defget_options
(self,section)
: opts = self.cf.options(section=section)
return opts
# 獲取section下的所有鍵值對
defget_kvs
(self,section)
: kvs = self.cf.items(section=section)
return kvs
# 根據section和option獲取指定的value
defget_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)
''' 基礎寫入配置檔案
-write(fp) 將config物件寫入至某個 .init 格式的檔案 write an .ini-format representation of the configuration state.
-add_section(section) 新增乙個新的section
-set(section, option, value) 對section中的option進行設定,需要呼叫write將內容寫入配置檔案 configparser2
-remove_section(section) 刪除某個 section
-remove_option(section, option) 刪除某個 section 下的 option
'''class
write_ini()
:def
__init__
(self,path,module)
:# 例項化配置物件
self.cf = configparser.configparser(
)# 獲取寫入檔案路徑,若採用w+方式則該檔案可以不存在
self.path = path
# 配置寫入方式,寫入方式"w+"清空寫
self.module = module
# 寫入配置檔案
defwrite_ini_file
(self)
:with
open
(self.path,self.module)
as f:
self.cf.write(f)
# 新增section
defadd_section
(self,section)
: self.cf.add_section(section=section)
self.write_ini_file(
)# 刪除某個 section
defremove_section
(self,section)
: self.cf.remove_section(section=section)
self.write_ini_file(
)# 刪除某個 section 下的 option
defremove_option
(self,section,option)
: self.cf.remove_option(section=section,option=option)
self.write_ini_file(
)if __name__ ==
'__main__'
:pass
操作INI配置檔案 vc
1.ini ini檔案被用來對作業系統或特定程式初始化或進行引數設定。2.優勢 ini有自己特定的格式,不用連同格式一起寫入檔案 ini有自己特定的讀寫方式,讀取時方便快捷。3.實現 a 格式 setion time0 2013 01 13 count 1 b 使用 a 在配置檔案中寫入time0的...
配置檔案INI的操作
這個段 lpkeyname包含了乙個鍵的名字,沒有該鍵則建立,如果該引數為null,則整 個段,包括段中所有的項都將被刪除 lpstring是被寫入win.ini檔案的字串,如果 lpkeyname,lpctstr lpdefault,lptstr lpreturnedstring,dword ns...
Qt QSettings配置檔案ini操作
qsettings類提供了持久的 平台無關的應用程式設定。用於儲存和恢復應用程式的設定。它也支援自定義儲存型別。如果你需要的是乙個非永續性的基於記憶體結構,可以考慮使用qmap代替。qsettings format有兩種 qsettings nativeformat在windows平台可以讀寫win...