python解析ini、conf、cfg檔案
1.使用python自帶的ConfigParser模組:
#test.cfg檔案內容:
[sec_a]
a_key1 = 20
a_key2 = 10
[sec_b]
b_key1 = 121
b_key2 = b_value2
b_key3 = $r
b_key4 = 127.0.0.1
import ConfigParser
#生成config物件
conf = ConfigParser.ConfigParser()
#用config物件讀取配置檔案
conf.read("test.cfg")
#以列表形式返回所有的section
sections = conf.sections()
print 'sections:', sections #sections: ['sec_b', 'sec_a']
#得到指定section的所有option
options = conf.options("sec_a")
print 'options:', options #options: ['a_key1', 'a_key2']
#得到指定section的所有鍵值對
kvs = conf.items("sec_a")
print 'sec_a:', kvs #sec_a: [('a_key1', '20'), ('a_key2', '10')]
#指定section,option讀取值
str_val = conf.get("sec_a", "a_key1")
int_val = conf.getint("sec_a", "a_key2")
print "value for sec_a's a_key1:", str_val #value for sec_a's a_key1: 20
print "value for sec_a's a_key2:", int_val #value for sec_a's a_key2: 10
conf.set("sec_b", "b_key3", "new-$r")
#寫入指定section增加新option和值
conf.set("sec_b", "b_newkey", "new-value")
#增加新的section
conf.add_section('a_new_section')
conf.set('a_new_section', 'new_key', 'new_value')
#寫回配置檔案
conf.write(open("test.cfg", "w"))
2.ConfigParser的一些問題:
1,不能區分大小寫。
2,重新寫入的ini檔案不能保留原有 INI檔案的註釋。
3,重新寫入的ini檔案不能保持原有的順序。
4,不支援巢狀。
5,不支援格式校驗。
嘗試下:configobj模組
#讀檔案
from configobj import ConfigObj
config = ConfigObj(filename)
#
value1 = config['keyword1']
value2 = config['keyword2']
#
section1 = config['section1']
value3 = section1['keyword3']
value4 = section1['keyword4']
#
# you could also write
value3 = config['section1']['keyword3']
value4 = config['section1']['keyword4']
#寫檔案如下:
from configobj import ConfigObj
config = ConfigObj()
config.filename = filename
#
config['keyword1'] = value1
config['keyword2'] = value2
#
config['section1'] = {}
config['section1']['keyword3'] = value3
config['section1']['keyword4'] = value4
#
section2 = {
'keyword5': value5,
'keyword6': value6,
'sub-section': {
'keyword7': value7
}
}
config['section2'] = section2
#
config['section3'] = {}
config['section3']['keyword 8'] = [value8, value9, value10]
config['section3']['keyword 9'] = [value11, value12, value13]
#
config.write()
#test.cfg檔案內容:
[sec_a]
a_key1 = 20
a_key2 = 10
[sec_b]
b_key1 = 121
b_key2 = b_value2
b_key3 = $r
b_key4 = 127.0.0.1
讀配置檔案
import ConfigParser
#生成config物件
conf = ConfigParser.ConfigParser()
#用config物件讀取配置檔案
conf.read("test.cfg")
#以列表形式返回所有的section
sections = conf.sections()
print 'sections:', sections #sections: ['sec_b', 'sec_a']
#得到指定section的所有option
options = conf.options("sec_a")
print 'options:', options #options: ['a_key1', 'a_key2']
#得到指定section的所有鍵值對
kvs = conf.items("sec_a")
print 'sec_a:', kvs #sec_a: [('a_key1', '20'), ('a_key2', '10')]
#指定section,option讀取值
str_val = conf.get("sec_a", "a_key1")
int_val = conf.getint("sec_a", "a_key2")
print "value for sec_a's a_key1:", str_val #value for sec_a's a_key1: 20
print "value for sec_a's a_key2:", int_val #value for sec_a's a_key2: 10
#寫配置檔案
conf.set("sec_b", "b_key3", "new-$r")
#寫入指定section增加新option和值
conf.set("sec_b", "b_newkey", "new-value")
#增加新的section
conf.add_section('a_new_section')
conf.set('a_new_section', 'new_key', 'new_value')
#寫回配置檔案
conf.write(open("test.cfg", "w"))
2.ConfigParser的一些問題:
1,不能區分大小寫。
2,重新寫入的ini檔案不能保留原有 INI檔案的註釋。
3,重新寫入的ini檔案不能保持原有的順序。
4,不支援巢狀。
5,不支援格式校驗。
嘗試下:configobj模組
#讀檔案
from configobj import ConfigObj
config = ConfigObj(filename)
#
value1 = config['keyword1']
value2 = config['keyword2']
#
section1 = config['section1']
value3 = section1['keyword3']
value4 = section1['keyword4']
#
# you could also write
value3 = config['section1']['keyword3']
value4 = config['section1']['keyword4']
#寫檔案如下:
from configobj import ConfigObj
config = ConfigObj()
config.filename = filename
#
config['keyword1'] = value1
config['keyword2'] = value2
#
config['section1'] = {}
config['section1']['keyword3'] = value3
config['section1']['keyword4'] = value4
#
section2 = {
'keyword5': value5,
'keyword6': value6,
'sub-section': {
'keyword7': value7
}
}
config['section2'] = section2
#
config['section3'] = {}
config['section3']['keyword 8'] = [value8, value9, value10]
config['section3']['keyword 9'] = [value11, value12, value13]
#
config.write()
相關文章
- .NET程式配置檔案操作(ini,cfg,config)
- NPM酷庫047:ini,解析INI配置檔案NPM
- ini檔案解析c庫(iniparser)【轉】
- python ini 配置檔案處理Python
- Python之ini配置檔案詳解Python
- Python 使用ConfigParser操作ini配置檔案教程。Python
- 透過python讀取ini配置檔案Python
- 解析MySQL 配置檔案 my.cnf / my.ini 區別MySql
- python XML 檔案解析PythonXML
- 【自動化測試】Python 讀取 .ini 格式檔案Python
- Python常用配置檔案ini、json、yaml讀寫總結PythonJSONYAML
- MySql5.7配置檔案my.ini 設定 my.ini檔案路徑MySql
- Mysql 配置檔案 my.iniMySql
- php.ini 檔案在哪裡?PHP
- go 讀取.ini配置檔案Go
- MySQL檔案my.ini配置MySql
- java 讀寫 ini 配置檔案Java
- pytest配置檔案pytest.ini
- Python進階,ConfigParser:Python中對於ini格式的配置檔案的使用Python
- 神祕的.user.ini檔案
- 使用C#讀寫ini檔案C#
- MySQL配置檔案my.ini在哪MySql
- Windows10 VS2017 C++ ini解析(使用simpleini標頭檔案)WindowsC++
- nginx.conf 配置檔案詳解Nginx
- openresty/nginx配置多個conf檔案RESTNginx
- Android開發之音訊配置檔案audio_policy.conf解析全過程Android音訊
- Python解析XML檔案生成HTMLPythonXMLHTML
- Python的configparser模組讀取.ini檔案內容並輸出Python
- MySQL 配置檔案 (my.ini) 詳解MySql
- MySQL的my.ini檔案查詢MySql
- php.ini 檔案存放位置不對?PHP
- C#關於讀寫INI檔案C#
- PHP配置檔案詳解php.iniPHP
- VC++學習筆記---配置檔案(一) ini檔案和propritiesC++筆記
- 利用.user.ini檔案隱藏後門
- 使用IniEditor讀寫INI型別配置檔案型別
- PostgreSQL-訪問策略配置檔案pg_hba.conf檔案(八)SQL
- Nginx 配置檔案 nginx.conf 中文詳解Nginx
- Redis——Redis.conf檔案簡單詳解Redis