.ini文件的python处理

  

from configparser import ConfigParser

cfg=ConfigParser()
cfg.read('c:/mysql.ini')
print(cfg.sections())
print(cfg.has_section('client'))
print(cfg.items('mysqld'))
for k,v in cfg.items():
    print(k,v,type(v))

tmp=cfg.get('mysqld','port')
print(tmp,type(tmp))
print(cfg.get('mysqld','a'))
#print(cfg.get('mysqld','vbn'))
print(cfg.get('mysqld','vbn',fallback='bbbbbbbbbbbbbbbb'))
tmp=cfg.getint('mysqld','port')
print(tmp,type(tmp))

if cfg.has_section('jar'):
    cfg.remove_section('jar')

cfg.add_section('lag')
cfg.set('lag','lag1','1')
cfg.set('lag','lag2','zxc')

with open('mysql.ini','w') as f:
    cfg.write(f)

print(cfg.getint('lag','vbn'))
cfg.remove_option('lag','vbn')

with open('mysql.ini',mode='w') as f:
    cfg.write(f)
    

 

上一篇:601. 体育馆的人流量


下一篇:数据挖掘:分享两个Pandas使用小陷阱