国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python解析模塊(ConfigParser)使用方法

2019-11-25 18:35:54
字體:
來源:轉載
供稿:網友

測試配置文件test.conf內容如下:

復制代碼 代碼如下:

[first]
w = 2
v: 3
c =11-3

[second]

sw=4
test: hello

測試配置文件中有兩個區域,first和second,另外故意添加一些空格、換行。

下面解析:

復制代碼 代碼如下:

>>> import ConfigParser
>>> conf=ConfigParser.ConfigParser()
>>> conf.read('test.conf')
['test.conf']
>>> conf.sections()   #獲得所有區域
['first', 'second']
>>> for sn in conf.sections():
...     print conf.options(sn)       #打印出每個區域的所有屬性
...
['w', 'v', 'c']
['sw', 'test']

獲得每個區域的屬性值:

復制代碼 代碼如下:

for sn in conf.sections():
    print sn,'-->'
    for attr in conf.options(sn):
        print attr,'=',conf.get(sn,attr)

輸出:

復制代碼 代碼如下:

first -->
w = 2
v = 3
c = 11-3
second -->
sw = 4
test = hello

好了,以上就是基本的使用過程,下面是動態的寫入配置,

復制代碼 代碼如下:

cfd=open('test2.ini','w')
conf=ConfigParser.ConfigParser()
conf.add_section('test')         #add a section
conf.set('test','run','false')  
conf.set('test','set',1)
conf.write(cfd)
cfd.close()

上面是向test2.ini寫入配置信息。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 珲春市| 临颍县| 寻乌县| 惠水县| 鱼台县| 安图县| 梁河县| 苍南县| 徐州市| 中牟县| 临澧县| 竹北市| 师宗县| 黔东| 盐池县| 天镇县| 故城县| 永川市| 吉木乃县| 新津县| 贡嘎县| 滨州市| 大城县| 东方市| 贵德县| 成安县| 四平市| 绥化市| 广丰县| 东山县| 汉川市| 霍山县| 阿荣旗| 伊吾县| 新民市| 洪雅县| 钟祥市| 茌平县| 定兴县| 体育| 米林县|