常见配置文件格式如下:
1 [DEFAULT] 2 serveraliveinterval = 45 3 compression = yes 4 compressionlevel = 9 5 forwardx11 = yes 6 7 [bitbucket.org] 8 user = hg 9 10 [topsecret.server.com] 11 host port = 50022 12 forwardx11 = no1)、读取配置组
1 >>> import configparser 2 >>> config = configparser.ConfigParser() 3 >>> config.sections() #不读取配置文件,组名列表为空 4 [] 5 >>> config.read("example.ini") #读取配置文件,返回配置文件名 6 ['example.ini'] 7 >>> config.sections() #返回除默认配置组的其他组名 8 ['bitbucket.org', 'topsecret.server.com'] 9 >>> config.defaults() #读取默认配置组,并返回有序字典 10 OrderedDict([('compressionlevel', '9'), ('serveraliveinterval', '45'), ('compression', 'yes'), ('forwardx11', 'yes')])2)、组名是否存在
1 >>> 'bitbucket.org' in config #组名存在 2 True 3 >>> 'zhangqigao.org' in config #组名不存在 4 False
3)、读取组内的值
1 >>> config["bitbucket.org"]["User"] #读取"bitbucket.org"配置组中的值 2 'hg' 3 >>> config["DEFAULT"]["Compression"] #读取默认配置组中的值 4 'yes' 5 >>> topsecret = config['topsecret.server.com'] #把配置组赋给一个对象 6 >>> topsecret['ForwardX11'] #通过对象获取值
4)、 循环获取组内的key值
1 >>> for key in config["bitbucket.org"]: #循环打印bitbucket.org组下的key值 2 ... print(key) 3 ... 4 #输出,只打印默认组和bitbucket.org组的key值 5 user 6 compressionlevel 7 serveraliveinterval 8 compression 9 forwardx11 10 >>> for key in config["topsecret.server.com"]:#循环打印topsecret.server.com组下的key值 11 ... print(key) 12 ... 13 #输出,只打印默认组和topsecret.server.com组的key值 14 host port 15 forwardx11 16 compressionlevel 17 serveraliveinterval 18 compression重点:
1、配置文件名i.cfg
1 2 3 4 5 6 7 8 9 10 [DEFAULT] k1 = v1 k2 = v2 [section1] k3 = v3 k4:v4 [section2] k5 = 52、读i.cfg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import configparser config = configparser.ConfigParser() config.read( "i.cfg" ) sec = config.sections() print (sec) #输出 [ 'section1' , 'section2' ] options = config.options( "section2" ) #返回默认组和section2组的key值 print (options) #输出 [ 'k5' , 'k1' , 'k2' ] item_list = config.items( "section2" ) #返回默认组和section2组的key-value值 print (item_list) #输出 [( 'k1' , 'v1' ), ( 'k2' , 'v2' ), ( 'k5' , '5' )] val1 = config.get( "section2" , "k1" ) #获取section2组中k1对应的值,是否可取是按照上面返回的列表 print (val1) #输出 v1 val2 = config.getint( "section2" , "k5" ) #返回section2中k5的值,这个值返回的int类型的 print (val2) #输出 53、改写i.cfg
①删除section和option
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import configparser config = configparser.ConfigParser() config.read( "i.cfg" ) config.remove_option( "section1" , "k3" ) #删除section1组下的k3 config.remove_section( "section2" ) #删除section2组 with open ( "i.cfg2" , "w" ) as f: #重新写入一个文件 config.write(f) #输出,写入文件的内容 [DEFAULT] k1 = v1 k2 = v2 [section1] k4 = v4②添加section
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import configparser config = configparser.ConfigParser() config.read( "i.cfg" ) sec = config.has_option( "section2" , "k5" ) #是否存在section2组内有k5 print (sec) #输出 True sec = config.has_section( "duoduo" ) #是否存在duoduo组 print (sec) #输出 False config.add_section( "duoduo" ) #添加section组duoduo config.add_section( "duoduo" ) #重新写入到一个配置文件中 with open ( "i.cfg3" , "w" ) as f: config.write(f)③添加或者设置option
1 2 3 4 5 6 7 8 9 import configparser config = configparser.ConfigParser() config.read( "i.cfg" ) config. set ( "duoduo" , "z" , "18" ) #设置或者添加duoduo中option值 with open ( "i.cfg3" , "w" ) as f: #重新写入文件中 config.write(f)
转载于:https://www.cnblogs.com/ManyQian/p/8353436.html
相关资源:JAVA上百实例源码以及开源项目