常用模块(四)

八、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学习 :常用模块(四)----- 配置文档的更多相关文章

  1. python学习——常用模块

    在学习常用模块时我们应该知道模块和包是什么,关于模块和包会单独写一篇随笔,下面先来了解有关在python中的几个常用模块. 一.什么是模块 常见的场景:一个模块就是一个包含了python定义和声明的文 ...

  2. 三、python学习-常用模块

    一.常用模块 1.math数学模块 在计算机中,所有数值在计算机底层都是约等于机制,并不是精确地 import math #ceil() 向上取整操作 math.ceil(3.1)=>4 #fl ...

  3. python常用模块-配置文档模块(configparser)

    python常用模块-配置文档模块(configparser) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. ConfigParser模块用于生成和修改常见配置文档,当前模块的名称 ...

  4. Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)

    OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname")  改变当前脚本工作目 ...

  5. Python学习——python的常用模块

    模块:用一堆代码实现了某个功能的代码集合,模块是不带 .py 扩展的另外一个 Python 文件的文件名. 一.time & datetime模块 import time import dat ...

  6. Python之常用模块学习(一)

    本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...

  7. configparser模块——配置文档

    configparser模块用于生成和修改常见配置文档. 预制配置文件:conf.ini [DEFAULT] ServerAliveInterval = 45 Compression = yes Co ...

  8. configparser模块——用于生成和修改常见配置文档

    配置文档格式 [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [b ...

  9. Python自动化 【第五篇】:Python基础-常用模块

    目录 模块介绍 time和datetime模块 random os sys shutil json和pickle shelve xml处理 yaml处理 configparser hashlib re ...

随机推荐

  1. .NET中怎么有效的使用Cache

    Cache 即高速缓存 ,我想非常多人对他的第一印象一定像我相同,感觉他一定能提高系统得性能和运行速度.的确.Net推出cache的初衷确实是这样的.那么cache是怎么提高系统性能和运行速度呢?是不 ...

  2. 【转】snmpwalk常用用法

    在日常监控中,经常会用到snmp服务,而snmpwalk命令则是测试系统各种信息最有效的方法,现总结一些常用的方法如下: 1.snmpwalk -v 2c -c public 10.103.33.1 ...

  3. The Shapes of CSS(css的形状)

    All of the below use only a single HTML element. Any kind of CSS goes, as long as it's supported in ...

  4. Android App性能优化(一)之布局优化

    当创建复杂布局的时候,我们会在xml 文件中添加大量的ViewGroup和View.伴随着每次迭代,View树的层次越来越深,界面加载速度越来越慢,消耗的内存也越来越多.当您的程序出现加载时短暂黑屏或 ...

  5. scala简介

    1.什么是Scala scala官方网址: http://www.scala-lang.org Scala是一种多范式的编程语言,其设计的初衷是要集成面向对象编程和函数式编程的各种特性.Scala运行 ...

  6. 中石油大学统考(大学英语B)押题笔记

    二. 词汇与结构 1. I will.意为“我会的”,固定搭配. 2. get tired of 是词组“对…厌烦了”的意思. 3. — ________ is your girl friend li ...

  7. 2018-2019-2 网络对抗技术 20165322 Exp1 PC平台逆向破解

    2018-2019-2 网络对抗技术 20165322 Exp1 PC平台逆向破解 目录 知识点总结 实验准备 任务一:直接修改程序机器指令,改变程序执行流程 任务二 通过构造输入参数,造成BOF攻击 ...

  8. Microsoft visual c++ 14.0 is required问题

    错误信息: error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Too ...

  9. SpringBoot实战(五)之Thymeleaf

    Thymeleaf同jsp.volocity.freemarker等共同的职能是MVC模式中的视图展示层,即View. 当然了,SpringBoot中也可以用jsp,不过不推荐这种用法,比较推崇的就是 ...

  10. C/S模式,发布/订阅模式和PUSH/PULL模式(上)

    CS模式(客户端/服务器模式) 最场景的信息传递模式,也称为Request/Response模式,或者调用模式.http/https协议即此模式.因为最常用所以大家一般都比较熟悉,这里不重点讲了,大家 ...