0081-【Python包】-ConfigObj-參數配置文件

安裝

pip install configobj

資源

github: https://github.com/DiffSK/configobj/blob/master/docs/configobj.rst
幫助手冊: https://configobj.readthedocs.io/en/latest/configobj.html


github官網例子

$cat write.py
from configobj import ConfigObj
config = ConfigObj()
config.filename = "./write1.config"
# 一級結構寫入
config['keyword1'] = "aaa"
config['keyword2'] = "bbb"
# 二級結構寫入
config['section1'] = {}
config['section1']['keyword3'] = 111
config['section1']['keyword4'] = 222
# 三級結構寫入
section2 = {
    'keyword5': "ccc",
    'keyword6': "ddd",
    'sub-section': {
        'keyword7': "eee"
        }
}
config['section2'] = section2
# 列表寫入
config['section3'] = {}
config['section3']['keyword 8'] = [111, 222, 333]
config['section3']['keyword 9'] = [444, 555, 666]
#
config.write()

顯示

$cat write1.config
keyword1 = aaa
keyword2 = bbb
[section1]
keyword3 = 111
keyword4 = 222
[section2]
keyword5 = ccc
keyword6 = ddd
[[sub-section]]
keyword7 = eee
[section3]
keyword 8 = 111, 222, 333
keyword 9 = 444, 555, 666

$cat read1.py
from configobj import ConfigObj
config = ConfigObj("write1.config")
#
value1 = config['keyword1']
value2 = config['keyword2']
print("value1 :",value1)
print("value2 :",value2)
#
section1 = config['section1']
value3 = section1['keyword3']
value4 = section1['keyword4']
print("section1 :",section1)
print("value3 :",value3)
print("value4 :",value4)
#
# you could also write
value3 = config['section1']['keyword3']
value4 = config['section1']['keyword4']
print("value3 :",value3)
print("value4 :",value4)

顯示

$python read1.py
value1 : aaa
value2 : bbb
section1 : {'keyword3': '111', 'keyword4': '222'}
value3 : 111
value4 : 222
value3 : 111
value4 : 222

config

關鍵字和值之間用“=”分隔,而部分標記位於方括號之間。 關鍵字,值和節名稱可以用單引號或雙引號括起來。 縮進並不重要,但可以保留。

通過重複節標記中的方括號來指示小節。 您可以使用更多括號來嵌套級別。

您可以通過使用逗號分隔項目來獲取列表值,並使用三引號(單引號或雙引號)跨越多行。

# This is the 'initial_comment'
# Which may be several lines
keyword1 = value1
'keyword 2' = 'value 2'

[ "section 1" ]
# This comment goes with keyword 3
keyword 3 = value 3
'keyword 4' = value4, value 5, 'value 6'

    [[ sub-section ]]    # an inline comment
    # sub-section is inside "section 1"
    'keyword 5' = 'value 7'
    'keyword 6' = '''A multiline value,
that spans more than one line :-)
The line breaks are included in the value.'''

        [[[ sub-sub-section ]]]
        # sub-sub-section is *in* 'sub-section'
        # which is in 'section 1'
        'keyword 7' = 'value 8'

[section 2]    # an inline comment
keyword8 = "value 9"
keyword9 = value10     # an inline comment
# The 'final_comment'
# Which also may be several lines

ConfigObj specifications參數

config = ConfigObj(infile=None, options=None, configspec=None, encoding=None,
                   interpolation=True, raise_errors=False, list_values=True,
                   create_empty=False, file_error=False, stringify=True,
                   indent_type=None, default_encoding=None, unrepr=False,
                   write_empty_values=False, _inspec=False)


案例

創建配置文件

$cat test.conf
[server]
servername = 192.168.11.1
serverport = 8000

[client_srv]
# 這裏是註釋
server = localhost
port = 8000


讀取配置文件

$cat read.py
from configobj import ConfigObj
#
conf_ini = "./test.config"
config = ConfigObj(conf_ini,encoding='UTF8')
#
# 讀配置文件
#
print(config['server'])
print(config['server']['servername'])

顯示結果:
如果方括號下面有多個選項,則爲字典,如果索引到終選項,則爲文本

$python read.py
{'servername': '192.168.11.1', 'serverport': '8000'}
192.168.11.1

修改文件

from configobj import ConfigObj
#
conf_ini = "./test.ini"
config = ConfigObj(conf_ini,encoding='UTF8')
config['server']['servername'] = "127.0.0.1"
config.write()

顯示

$cat test.config
[server]
servername = 127.0.0.1


添加新項

from configobj import ConfigObj
#
conf_ini = "./test.ini"
config = ConfigObj(conf_ini,encoding='UTF8')
config['new_items'] = {}
config['new_items']['Items1'] = "test items"
config.write()

顯示

[new_items]
Items1 = test items

刪除某一項

from configobj import ConfigObj
#
conf_ini = "./test.ini"
config = ConfigObj(conf_ini,encoding='UTF8')
del config['client_srv']['port']
config.write()

顯示

[client_srv]
# 這裏是註釋
server = localhost

將配置文件寫入到不同的文件中

from configobj import ConfigObj
#
conf_ini = "./test.ini"
config = ConfigObj(conf_ini,encoding='UTF8')
config.filename = "./test1.ini"
config.write()

顯示

$cat test1.config
[server]
servername = 127.0.0.1
serverport = 8000

[client_srv]

server = localhost
[new_items]
Items1 = test items


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章