Python ConfigParser 注意事項

2021-07-27 22:14:17 字數 3235 閱讀 7553

以這個非常簡單的典型配置檔案為例:

[default]

serveraliveinterval = 45

compression = yes

compressionlevel = 9

forwardx11 = yes

[bitbucket.org]

user = hg

[topsecret.server.com]

port = 50022

forwardx11 = no

1、config parser 操作跟dict 類似,在資料訪問方法基本一致

>> import configparser

>>> config = configparser.configparser()

>>> config.sections()

>>> config.read('example.ini')

['example.ini']

>>> config.sections()

['bitbucket.org', 'topsecret.server.com']

>>> 'bitbucket.org' in config

true

>>> 'bytebong.com' in config

false

>>> config['bitbucket.org']['user']

'hg'

>>> config['default']['compression']

'yes'

>>> topsecret = config['topsecret.server.com']

>>> topsecret['forwardx11']

'no'

>>> topsecret['port']

'50022'

>>> for key in config['bitbucket.org']: print(key)

...user

compressionlevel

serveraliveinterval

compression

forwardx11

>>> config['bitbucket.org']['forwardx11']

'yes'

[****** values]

key=value

spaces in keys=allowed

spaces in values=allowed as well

spaces around the delimiter = obviously

you can also use : to delimit keys from values

[all values are strings]

values like this: 1000000

or this: 3.14159265359

are they treated as numbers? : no

integers, floats and booleans are held as: strings

can use the api to get converted values directly: true

[multiline values]

chorus: i'm a lumberjack, and i'm okay

i sleep all night and i work all day

[no values]

key_without_value

empty string value here =

[you can use comments]

# like this

; or this

# by default only in an empty line.

# inline comments can be harmful because they prevent users

# from using the delimiting characters as parts of values.

# that being said, this can be customized.

[sections can be indented]

can_values_be_as_well = true

does_that_mean_anything_special = false

purpose = formatting for readability

multiline_values = are

handled just fine as

long as they are indented

deeper than the first line

of a value

# did i mention we can indent comments, too?

常見做法:

config.write(open('example.ini', 'w'))
合理做法:

with open('example.ini', 'w') as configfile:

config.write(configfile)

configparser 在get 時會自動過濾掉『#』或『;『注釋的行(內容);

一般情況下我們手工會把配置中的暫時不需要的用『#『注釋,問題在於,configparser 在wirte的時候同file object行為一致,如果將注釋』#『的配置經過get後,再wirte到conf,那麼』#『的配置就會丟失。

那麼就需要乙個策略或規則,配置需不需要手工編輯 ?還是建立複雜的對原生文字的處理的東西,我建議是管住手,避免將一些重要的配置爆露給使用者編輯,切記行內注釋和section內注釋。

有乙個相對簡單的方法是:

對單獨在一行的**,你可以在讀入前把"#", ";"換成其他字元如』@』,或『^』(在其bat等其他語言中用的注釋符易於理解),使用allow_no_value選項,這樣注釋會被當成配置儲存下來,處理後你再把「#」, ";"換回來。

在configparser write之後,配置文字如果有大寫字母』product』會變為小寫字母』product』,並不影響配置的正確讀寫。

待續。。。。。。

python ConfigParser模組詳解

功能介紹 在程式中使用配置檔案來靈活的配置一些引數是一件很常見的事情,配置檔案的解析並不複雜,在python裡更是如此,在官方發布的庫中就包含有做這件事情的庫,那就是configparser,這裡簡單的做一些介紹。configparser解析的配置檔案的格式比較象ini的配置檔案格式,就是檔案中由多...

python configparser配置檔案

通過配置檔案來管理不怎麼經常改變的引數,同時保證下次修改又能很快捷,就可以使用ini的配置來管理。以python3為作為直譯器,對應的包是configparser 配置檔案config.ini email qq sender zhangx mail.163.cn port 45smtp server...

python ConfigParser模組介紹

python讀配置檔案的包 configparser configparser模組在python中用來讀取配置檔案,配置檔案的格式跟windows下的ini配置檔案相似,可以包含乙個或多個節 section 每個節可以有多個引數 鍵 值 使用的配置檔案的好處就是不用在程式設計師寫死,可以使程式更靈活...