.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)
from configparser import ConfigParser
cfg=ConfigParser()
cfg.read('c:/z.ini')
print(cfg.sections())
print(cfg.has_section('jar'))
# print(cfg.items('mysqld'))
for section in cfg.sections():
for option in cfg.options(section):
print(section,option,type(option))
# for k,v in cfg.items('mysqld'):
# print(k,type(v))
for k,v in cfg.items():
print(k,v,type(v))
if not cfg.has_section('jar'):
cfg.add_section('jar')
# cfg.set('jar','jar1','123')
# cfg.set('jar','jar2','abc')
# print(cfg.sections())
#
# with open('c:/z.ini',mode='wt+') as f:
# cfg.write(f)
print(cfg.sections())
print(cfg.options('jar'))
print(type(cfg.get('jar','jar1')))
b=cfg.getint('jar','jar1')
print(b,type(b))
b=cfg.get('jar','a')
print(b)
print(cfg.get('mysqld','vbn',fallback='bbbbbbbbbbbbbbbbbbb'))
if cfg.has_section('jar'):
cfg.remove_section('jar')
with open('c:/z.ini',mode='wt+') as f:
cfg.write(f)