Python使用ConfigParser模块操作配置文件的方法


Posted in Python onJune 29, 2018

本文实例讲述了Python使用ConfigParser模块操作配置文件的方法。分享给大家供大家参考,具体如下:

一、简介

用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser

二、配置文件格式

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no

三、创建配置文件

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)

四、读取配置文件

1、读取节点信息

import configparser
config = configparser.ConfigParser()
config.read('example.ini')
# 读取默认配置节点信息
print(config.defaults())
#读取其他节点
print(config.sections())

输出

OrderedDict([('compression', 'yes'), ('serveraliveinterval', '45'), ('compressionlevel', '9'), ('forwardx11', 'yes')])
['bitbucket.org', 'topsecret.server.com']

2、判读配置节点名是否存在

print('ssss' in config)
print('bitbucket.org' in config)

输出

False
True

3、读取配置节点内的信息

print(config['bitbucket.org']['user'])

输出

hg

4.循环读取配置节点全部信息

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

输出

user : hg
compression : yes
serveraliveinterval : 45
compressionlevel : 9
forwardx11 : yes

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python使用爬虫猜密码
Feb 19 Python
Python内置的HTTP协议服务器SimpleHTTPServer使用指南
Mar 30 Python
Python的Flask框架中集成CKeditor富文本编辑器的教程
Jun 13 Python
使用Turtle画正螺旋线的方法
Sep 22 Python
Python实现的当前时间多加一天、一小时、一分钟操作示例
May 21 Python
python实现txt文件格式转换为arff格式
May 31 Python
django搭建项目配置环境和创建表过程详解
Jul 22 Python
python自动化工具之pywinauto实例详解
Aug 26 Python
Django项目中使用JWT的实现代码
Nov 04 Python
python关于调用函数外的变量实例
Dec 26 Python
利用Python实现学生信息管理系统的完整实例
Dec 30 Python
python模块与C和C++动态库相互调用实现过程示例
Nov 02 Python
python计算列表内各元素的个数实例
Jun 29 #Python
python判断设备是否联网的方法
Jun 29 #Python
python3 对list中每个元素进行处理的方法
Jun 29 #Python
python对list中的每个元素进行某种操作的方法
Jun 29 #Python
Python中交换两个元素的实现方法
Jun 29 #Python
python3中zip()函数使用详解
Jun 29 #Python
对python list 遍历删除的正确方法详解
Jun 29 #Python
You might like
第九节 绑定 [9]
2006/10/09 PHP
php SQL Injection with MySQL
2011/02/27 PHP
PHP记录搜索引擎蜘蛛访问网站足迹的方法
2015/04/15 PHP
php-fpm开启状态统计的方法详解
2017/06/23 PHP
IE 条件注释详解总结(附实例代码)
2009/08/29 Javascript
$.format,jquery.format 使用说明
2011/07/13 Javascript
Extjs中ComboBoxTree实现的下拉框树效果(自写)
2013/05/28 Javascript
js arguments,jcallee caller用法总结
2013/11/30 Javascript
node.js中的path.dirname方法使用说明
2014/12/09 Javascript
JavaScript页面模板库handlebars的简单用法
2015/03/02 Javascript
jQuery简单实现遍历数组的方法
2015/04/14 Javascript
jquery实现的代替传统checkbox样式插件
2015/06/19 Javascript
详解JavaScript逻辑And运算符
2015/12/04 Javascript
Backbone View 之间通信的三种方式
2016/08/09 Javascript
JS简单实现仿百度控制台输出信息效果
2016/09/04 Javascript
React Router基础使用
2017/01/17 Javascript
jQuery实现简单复制json对象和json对象集合操作示例
2018/07/09 jQuery
vue 实现滚动到底部翻页效果(pc端)
2019/07/31 Javascript
深入学习python的yield和generator
2016/03/10 Python
pandas 条件搜索返回列表的方法
2018/10/30 Python
python2使用bs4爬取腾讯社招过程解析
2019/08/14 Python
Python中if有多个条件处理方法
2020/02/26 Python
python模拟实现分发扑克牌
2020/04/22 Python
Html5实现文件异步上传功能
2017/05/19 HTML / CSS
我的applet原先好好的, 一放到web server就会有问题,为什么?
2016/05/10 面试题
广州地球村科技数据库题目
2016/04/25 面试题
Linux中如何用命令创建目录
2016/12/02 面试题
总经理职责范文
2013/11/08 职场文书
房地产销售大学生自我评价分享
2013/11/11 职场文书
信息技术教学反思
2014/02/12 职场文书
原料仓仓管员岗位职责
2014/07/08 职场文书
领导干部对照检查材料
2014/08/24 职场文书
python3美化表格数据输出结果的实现代码
2021/04/14 Python
利用python调用摄像头的实例分析
2021/06/07 Python
利用Python实现Picgo图床工具
2021/11/23 Python
OpenFeign实现远程调用
2022/08/14 Java/Android