python--configparser模块

import configparser
该模块适用于配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),节用[]括住,每个节可以有多个参数(键=值)。
常见文档格式如下:
[DEFAULT]     #意思是[默认]节,[DEFAULT]固定有这种写写法,其他节的名字可以随便命名,在其它节被for循环打印的时候,都会打印这个默认的节,但是读配置文件的的时候默认不读这个节的名字
ServerAliveInterval = 45
Compression = yes        #压缩=是
CompressionLevel = 9    #压缩级别= 9
ForwardX11 = yes             #转发X11 =是
[bitbucket.org]    #也是节
User = hg

[topsecret.server.com] #也是节
Host Port = 50022 #主机端口
ForwardX11 = no # 转发X11
 
(一) 用python生成一个这样的文档
import configparser
config=configparser.ConfigParser()    #拿到一个configparser模块操作句柄config
config['DEFAULT']={'ServerAliveInterval': '45',
                 'Compression': 'yes',
                'CompressionLevel':'9 ',
                 'ForwardX11':'yes'}
config['bitbucket.org'] = {'User':'hg'}

config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}
with open('example.ini','w') as configfile:   #打开一个example.ini配置文件,为了后面往里面写内容
    config.write(configfile)     # 用configparser方法写configfile(即example.ini)配置文件内容
View Code
(二)查找文件  -------->查找文件内容,基于字典的形式
import  configparser
config=configparser.ConfigParser()    #记得最后加括号
print(config.sections())      #[]       sections  节点
2.1查看节
config.read('example.ini')   #先把配置文件读出来
print(config.sections())     #['bitbucket.org', 'topsecret.server.com'
2.2查看是否有某节
print('topsecret.server.com' in config)     #True 判断某个节是否是config

2.3查看配置项 for 循环 config.options items功能是一模一样的

"""
区别:for 循环           打印出来是字符串
      config.options     打印出来是列表
      items              打印出来是元祖,并且连着配置项的值也打印出来了
"""
print(config['topsecret.server.com']['Host Port'])        #50022
print(config['topsecret.server.com'])    #<Section: topsecret.server.com>
for k in config['topsecret.server.com']:
    print(k)           #不仅仅会打印被循环的节里的配置项,还会打印默认[DEFAULT]节的配置项
                                    host port
                                    forwardx11
                                    compression
                                    serveraliveinterval
                                    compressionlevel
print(config.options('topsecret.server.com'))    #['host port', 'forwardx11', 'compression', 'compressionlevel', 'serveraliveinterval']
print(config.items('topsecret.server.com'))    #找到'bitbucket.org'下所有键值对
[('serveraliveinterval', '45'), ('compression', 'yes'), ('forwardx11', 'no'), ('compressionlevel', '9'), ('host port', '50022')]

get方法Section下的key对应的value
print(config.get('topsecret.server.com','ForwardX11'))    # no
View Code
(三)增删改操作
import configparser
config=configparser.ConfigParser()
config.read('example.ini')

3.1增

config.add_section('func_you')
print(config.sections())              #['bitbucket.org', 'topsecret.se 

3.2删

config.remove_section('func_you')
print(config.sections())      #['bitbucket.org', 'topsecret.server.com']
config.remove_option('bitbucket.org','User')
print(config.options('bitbucket.org'))    #['forwardx11', 'compression', 'serveraliveinterval', 'compressionlevel']
#已经删除了,查到只有[DEFAULT] 的内容了

3.3改

config.set('bitbucket.org','User','zzy')
for k in config['bitbucket.org']:
    print(k)
f = open(''me.ini'', "w")
config.write(f) # 写进文件
f.close()
原文地址:https://www.cnblogs.com/zzy-9318/p/8379679.html