Python 使用ConfigParser操作配置文件

常用方法

读取配置文件

  • read(filename) 读取配置文件内容
  • sections() 得到所有的section,并以列表的形式返回
  • options(section) 得到指定section的所有option
  • items(section) 得到指定section的所有键值对
  • get(section,option) 得到section中option的值,返回为string类型
  • getint(section,option) 得到section中option的值,返回为int类型
  • getboolean(section,option) 得到指定section中option的值,返回为boole类型   可以识别: 'yes'/'no', 'on'/'off', 'true'/'false' and '1'/'0'

判断section, option

  • has_section(section) 判断配置文件中是否有指定的section, 返回True或False
  • has_option(section, option) 判断指定的section中是否有指定的option, 返回True或False

写入配置文件

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

修改配置文件

  • add_section(sections) 添加一个sections
  • set(section,option,value) 向指定sections中添加option和值
  • remove_option(section,option) 删除指定sections中的option
  • remove_section(section) 删除指定sections, 其中的option也被删除

conf文件内容:

[sections1]
username = root
is_admin = yes
passwd = 9527

[sections2]
ip = 192.168.1.1

[sections3]
port = 22

代码:

# -*- coding: utf-8 -*-
import configparser
config = configparser.ConfigParser()
# 读取配置文件里面的数据
config.read('conf', encoding='utf-8')  

# 查看conf配置文件中的配置项(配置模块的名字)  注意: 若配置模块的名是 DEFAULT 不能被获取
sec = config.sections()
print(sec)  # ['sections1', 'sections2', 'sections3']

# 指定section的所有键值对
options_items = config.items('sections1')
print(options_items)  # [('username', 'root'), ('is_admin', 'yes'), ('passwd', '9527')]

# 获取指定section的所有option
opt = config.options('sections1')
print(opt)  # ['username', 'is_admin', 'passwd']

for key in config['sections1']:
    print(key)  # 'username', 'is_admin', 'passwd'

# 获取指定section中option的值,返回为string类型
username = config.get('sections1', 'username')
print(username)  # root

username = config['sections1'].get('username')
print(username)  # root

# 获取指定section中option的值,返回为int类型
passwd = config.getint('sections1', 'passwd')
print(passwd)  # 9527

# 获取指定section中option的值,返回为boole类型
is_admin = config.getboolean('sections1', 'is_admin')
print(is_admin)  # True

# 判断配置文件中是否有指定的section, 返回True或False
is_exist = config.has_section('sections1')
print(is_exist)  # True

# 判断指定的section中是否有指定的option, 返回True或False
is_exist = config.has_option('sections1', 'username')
print(is_exist)  # True

# 写入配置文件
config['sections1'] = {'name' : 'root',
                     'is_admin' : 'no',
                     'passwd' : '9527'}

config['sections2'] = {}
config['sections2']['ip'] = '192.168.1.1'

config['sections3'] = {}
topsecrect = config['sections3']

topsecrect['port'] = '22'

with open('example.txt', 'w') as configfile:
    config.write(configfile)

# 修改配置文件
# 添加一个section
config.add_section('sections4')
config.set('sections4', 'path', '/home/kkk/')
config.write(open('conf', 'w'))

# 设置(修改)某个option 的值
config.set('sections4', 'path', '/home/MMMM/')
config.write(open('conf', 'w'))

# 移除section 或者option
config.remove_option('sections4', 'path')
config.remove_section('sections4')
config.write(open('conf', 'w'))

解决写入文件中的键名小写问题

python3 解决方案

config = configparser.RawConfigParser()
config.optionxform = lambda option: option
config.read('conf', encoding='utf-8')

python2  解决方案

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值