Python学习:configparser模块

配置文件
用户配置文件就是在用户登录电脑时,或是用户在使用软件时,软件系统为用户所要加载所需环境的设置和文件的集合。它包括所有用户专用的配置设置,如程序项目、屏幕颜色、网络连接、打印机连接、鼠标设置及窗口的大小和位置等。

该模块的作用 就是使用模块中的RawConfigParser()ConfigParser()SafeConfigParser()这三个方法(三者择其一),创建一个对象,使用对象的方法对指定的配置文件做增删改查 操作。

模块所解析的ini配置文件是由多个section构成,每个section名用中括号‘[]’包含,每个section下可有多个配置项类似于key-value形式,例如:

[bitbucket.org]
User = hg
  
[topsecret.server.com]
Port = 50022
ForwardX11 = no
  1. ConfigParser 初始化
    使用ConfigParser 首选需要初始化实例,并读取配置文件:
    cf = ConfigParser.ConfigParser() cf.read("配置文件名")

  2. 基本的读取配置文件

  • read(filename) 直接读取ini文件内容
  • sections() 得到所有的section,并以列表的形式返回
  • options(section) 得到该section的所有option
  • items(section) 得到该section的所有键值对
  • get(section,option) 得到section中option的值,返回为string类型
  • getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。

3.基本的写入配置文件

  • add_section(section) 添加一个新的section
  • set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。

用python生成一个配置文件

import configparser

#第一种创建方式
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                     'Compression': 'yes',
                     'CompressionLevel': '9'}

#第二种创建方式
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'

#第三种创建方式
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'  # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'

#文件的写入
with open('example.ini', 'w') as configfile:
    config.write(configfile)


在这里插入图片描述
读配置文件
在这里插入图片描述

  • 获得配置文件的分块
cp=configparser.ConfigParser()
cp.read('example.ini')
print(cp.sections())
#输出:['bitbucket.org', 'topsecret.server.com']
  • 获得配置文件中的某一特定分块的key值
print(cp.options('topsecret.server.com'))
#输出:['host port', 'forwardx11']
  • 获得配置文件中的某一特定分块的value值,返回一个列表,元素是元组(key,value)
print(cp.items('topsecret.server.com'))
#输出:[('host port', '50022'), ('forwardx11', 'no')]
  • 获得配置文件中具体某条配置信息
print(cp.get('topsecret.server.com','host port'))
#输出:50022
  • 写配置文件到某一分块
cp.set('bitbucket.org','test','123')
cp.write(open('example.ini','w'))

在这里插入图片描述

  • 新增某一分块
cp.add_section('hah')
cp.write(open('example.ini','w'))

在这里插入图片描述

  • 删除某一分块中配置
cp.remove_option('bitbucket.org','user')
cp.write(open('example.ini','w'))
  • 删除某一分块注意,删除某一分块会连其下的配置全部删除。
cp.remove_section('bitbucket.org')
cp.write(open('example.ini','w'))

其它

import configparser

config = configparser.ConfigParser()

#----------------------查
print(config.sections())   #[]

config.read('example.ini')

print(config.sections())   #['bitbucket.org', 'topsecret.server.com']

print('bytebong.com' in config)# False

print(config['bitbucket.org']['User']) # hg

print(config['DEFAULT']['Compression']) #yes

print(config['topsecret.server.com']['ForwardX11'])  #no


for key in config['bitbucket.org']:
    print(key)


# user
# serveraliveinterval
# compression
# compressionlevel
# forwardx11


print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']
print(config.items('bitbucket.org'))  #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]

print(config.get('bitbucket.org','compression'))#yes


#--------------------------删,改,增(config.write(open('i.cfg', "w")))


config.add_section('yuan')

config.remove_section('topsecret.server.com')
config.remove_option('bitbucket.org','user')

config.set('bitbucket.org','k1','11111')

config.write(open('i.cfg', "w"))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值