Python学习 :常用模块(四)----- 配置文档
常用模块(四)
八、configparser 模块
官方介绍:A configuration file consists of sections, lead by a "[section]" header,and followed by "name: value" entries,
with continuations and such in the style of RFC 822.
ConfigParse 模块在 python3 中修改为 configparse ,此模块常用于生成和修改常见配置文档
Eg.编辑文档的配置文件
常见的文档格式
[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'] = {'User' : 'hg'} # 进行文件配置的第二种方式
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # 在字典中分别添加键值对
topsecret['ForwardX11'] = 'no' config['DEFAULT']['ForwardX11'] = 'yes' # 为第一个字典添加键值对
with open('example.ini', 'w') as configfile:
config.write(configfile)
configparse 模块的相关操作
工作方式:先把配置文件读取出来,放入一个对象中再进行相应的操作
import configparser # 把配置文件放入一个对象中,进行读取操作
config = configparser.ConfigParser()
config.read('example.ini')
# 浏览配置文件中的内容
print(config.sections())
>>> ['bitbucket.org', 'topsecret.server.com']
print(config.defaults()) # defaults 为特殊的单元,需要单独进行操作
>>> OrderedDict([('compressionlevel', '9'), ('serveraliveinterval', '45'), ('compression', 'yes'), ('forwardx11', 'yes')]) # 对配置文件进行取值
print(config['bitbucket.org']['User'])
for key in config['bitbucket.org']: print(key) # default为特殊的单元,无论对谁进行打印它都会跟着
>>> hg
user
compressionlevel
serveraliveinterval
compression
forwardx11 # 删除数据的两种方法
config.remove_section('topsecret.server.com')
config.remove_option('bitbucket.org','user') config.set('bitbucket.org','user','Mike')
print(config.has_section('topsecret.server.com'))
>>> False # 修改数据
config.write(open('example.ini', "w")) # 文件一旦写入后不能修改,此操作为重新创建一个名字相同文件,并进行相应的增加、删除、修改操作
Python学习 :常用模块(四)----- 配置文档的更多相关文章
- python学习——常用模块
在学习常用模块时我们应该知道模块和包是什么,关于模块和包会单独写一篇随笔,下面先来了解有关在python中的几个常用模块. 一.什么是模块 常见的场景:一个模块就是一个包含了python定义和声明的文 ...
- 三、python学习-常用模块
一.常用模块 1.math数学模块 在计算机中,所有数值在计算机底层都是约等于机制,并不是精确地 import math #ceil() 向上取整操作 math.ceil(3.1)=>4 #fl ...
- python常用模块-配置文档模块(configparser)
python常用模块-配置文档模块(configparser) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. ConfigParser模块用于生成和修改常见配置文档,当前模块的名称 ...
- Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)
OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目 ...
- Python学习——python的常用模块
模块:用一堆代码实现了某个功能的代码集合,模块是不带 .py 扩展的另外一个 Python 文件的文件名. 一.time & datetime模块 import time import dat ...
- Python之常用模块学习(一)
本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...
- configparser模块——配置文档
configparser模块用于生成和修改常见配置文档. 预制配置文件:conf.ini [DEFAULT] ServerAliveInterval = 45 Compression = yes Co ...
- configparser模块——用于生成和修改常见配置文档
配置文档格式 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [b ...
- Python自动化 【第五篇】:Python基础-常用模块
目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...
随机推荐
- NAT原理简介、各种 ADSL Modem 及路由器的端口映射方法
NAT原理简介 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task Force ...
- [翻译] HSDatePickerViewController
HSDatePickerViewController HSDatePickerViewController is an iOS ViewController for date and time pic ...
- Linux操作NFS挂载、卸载等操作
一.NFS服务器的设置 NFS服务器的设定可以通过/etc/exports这个文件进行,设定格式如下 分享目录 主机名或IP(参数1,参数2) /binbin 172.17.1.*(rw,sync,n ...
- August 24th 2017 Week 34th Thursday
If you have choices, choose the best. If you have no choice, do the best. 如果有选择,那就选择最好的:如果没有选择,那就努力做 ...
- Monster: half man, half beast and very scary.
Monster: half man, half beast and very scary. 怪物,半人半兽很吓人.
- archer 安装
archer 项目地址: https://github.com/jly8866/archer 安装docker版本 Pull Docker docker pull hhyo/archer 启动服务do ...
- Linux下文件的打包、解压缩指令——tar,gzip,bzip2,unzip,rar
本文是笔者对鸟叔的Linux私房菜(基础学习篇) 第三版(中文网站)中关于 Linux 环境下打包和解压缩指令的内容以及日常操作过程中所接触的相关指令的总结和记录,以供备忘和分享.更多详细信息可直接参 ...
- 遍历Map集合的几种方式
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entr ...
- 通过163smtp服务器向各大邮箱发送邮件(SOCKET编程)
package server; import java.io.*; import java.net.*; import java.sql.Time; import java.util.Scanner; ...
- 匹配IP的正则表达式
正则表达式匹配IP 1 ((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[1-9])