Python基礎18模塊-configerparse模塊
阿新 • • 發佈:2018-10-23
port org ret ems code cti write serve nbsp
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‘))
Python基礎18模塊-configerparse模塊