python之常用模块ConfigParser

这个常见于.conf,.ini等类型的配置文件

下面先看一下如果通过python生成一个.ini文件

import configparser #2.x is ConfigParser
config = configparser.ConfigParser() #先生成一个对象
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
'''
config["DEFAULT"]自动生成根节点,等号右面的是这个节点下面的值,相当于DEFAULT是key,后面的是values
'''
config['bitbucket.org'] = {} #这是第二个key,value是空的
config['bitbucket.org']['User'] = 'hg' #这是给第二个key添加了一个键值对
config['topsecret.server.com'] = {} #这是第三个key
topsecret = config['topsecret.server.com'] #这是把这个key对应的值赋值给一个变量
topsecret['Host Port'] = '50022' # 添加key和value
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'#给DEFAULT赋值key和value
with open('example.ini', 'w') as configfile: #以w的形式打开example.ini文件
config.write(configfile) #把以上的内容写入这里,生成配置文件
-----------------上面是代码,下面是结果---------------------------
自动生成了一个example.ini文件,如下:
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
forwardx11 = yes

[bitbucket.org]
user = hg

[topsecret.server.com]
host port = 50022
forwardx11 = no

#----------------------以下是读取上面生成的配置文件-------------------
import configparser
config = configparser.ConfigParser()
config.read('example.ini')
#查看所有的标题
res = config.sections() #当第一个根节点是大写的DEFAULT的时候,这个方法是读取不出来DEFAULT标题的,更改成其他任意值就能读取出来。但是他会把DEFAULT内的所有的key/value都划分到下一个标题中
print(res) #['bitbucket.org', 'topsecret.server.com'] 所以这里输出的值只有两个
re = config.defaults() #这个方法会把DEFAULT的key/vaule全部打出来 OrderedDict([('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes')])
print(re)
#查看bitbucket.org标题下所有的key
options = config.options('bitbucket.org') #这个里面包含了DEFAULT的全部的key
print(options) #['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']

#查看bitbucket.org标题下所有的key-value,并以(key,value)形式输出
item_list = config.items('bitbucket.org')
print(item_list) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]

#获取bitbucket.org标题下user的值
val = config.get('bitbucket.org','user')
print(val) #hg
vall = config.get('bitbucket.org','serveraliveinterval')
print(vall) #45 这是获取的DEFAULT内的值

------------------------------------看如何修改配置文件---------------------------------
这是配置文件:
[section1]
k1 = v1
k2:v2
user=egon
age=18
is_admin=true
salary=31
[section2]
k1 = v1

这是代码:
import  configparser
config = configparser.ConfigParser()
config.read('config.conf',encoding='utf-8')

#删除整个标题section2
config.remove_section('section2')
config.write(open('config1.conf','w')) #删除之后要重新写入一个新的文件才能生效
#删除标题section1下的某个k1和k2
data1 = config.remove_option('section1','k1')
config.write(open('config1.conf','w'))

#判断是否存在某个标题
print(config.has_section('section1'))

#判断标题section1下是否有user
print(config.has_option('section1',''))

#添加一个标题
config.add_section('egon')

#在标题egon下添加name=egon,age=18的配置
config.set('egon','name','egon')
config.set('egon','age',18) #报错,必须是字符串

#最后将修改的内容写入文件,完成最终的修改
config.write(open('a.cfg','w'))


原文地址:https://www.cnblogs.com/hally/p/8319041.html