Python基础18模块-configerparse模块

1.congfigerparse简单应用

 1 import configparser
 2 config = configparser.ConfigParser()
 3 #
 4 config['bitbucket.org'] = {}
 5 config['bitbucket.org']['User'] = 'hg'
 6 config['topsecret.server.com'] = {}
 7 topsecret = config['topsecret.server.com']
 8 topsecret['Host Port'] = '5022'
 9 topsecret['ForwardX11'] = 'no'
10 config['DEFAULT']['ForwardX11'] = 'yes'
11 with open('example.ini','w') as f:
12     config.write(f)
13 config.read('example.ini')
14 #读,定义在DEFAULT里的东西会默认读出来
15 for key in config['bitbucket.org']:
16     print(key)
17 print(config.options('bitbucket.org'))
18 print(config.items('bitbucket.org'))
19 #增加
20 config.add_section('yuan')
21 config.set('bitbucket.org','k1','11111')
22 #删除
23 config.remove_section('topsecret.server.com')
24 config.remove_option('bitbucket.org','User')
25 config.write(open('example.ini','w'))

 

posted @ 2018-10-23 10:54  献丑小角  阅读(519)  评论(0编辑  收藏  举报