python小白-day6 ConfigParser模块
ConfigParser模块
用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。
来看一个好多软件的常见文档格式如下
用python生成上文档代码如下;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read( 'example.ini' )
[ 'example.ini' ]
>>> config.sections()
[ 'bitbucket.org' , 'topsecret.server.com' ]
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config[ 'bitbucket.org' ][ 'User' ]
'hg'
>>> config[ 'DEFAULT' ][ 'Compression' ]
'yes'
>>> topsecret = config[ 'topsecret.server.com' ]
>>> topsecret[ 'ForwardX11' ]
'no'
>>> topsecret[ 'Port' ]
'50022'
>>> for key in config[ 'bitbucket.org' ]: print (key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config[ 'bitbucket.org' ][ 'ForwardX11' ]
'yes'
|
删除bitbucket.org:
1
2
3
4
5
6
|
import configparser
config = configparser.ConfigParser()
config.read( 'example.ini' )
config.remove_section( 'bitbucket.org' )
config.write( open ( 'example.bak' , 'w' ))
|
增加hetan:
1
2
3
4
5
6
7
8
|
import configparser
config = configparser.ConfigParser()
config.read( 'example.ini' )
config.has_section( 'hetan' )
config.add_section( 'hetan' )
config[ 'hetan' ][ 'age' ] = "26"
config.write( open ( 'example.cfg' , 'w' ))
|
修改hetan中的age为25:
1
2
3
4
5
6
|
import configparser
config = configparser.ConfigParser()
config.read( 'example.cfg' )
config. set ( 'hetan' , 'age' , '25' )
config.write( open ( 'example.cfg.bak' , 'w' ))
|
删除hetan中的age:
1
2
3
4
5
6
|
import configparser
config = configparser.ConfigParser()
config.read( 'example.cfg.bak' )
config.remove_option( 'hetan' , 'age' )
config.write( open ( 'example.cfg.bak2' , 'w' ))
|
转载于:https://www.cnblogs.com/hetan/p/5211133.html
python小白-day6 ConfigParser模块相关推荐
- python:实例化configparser模块读写配置文件
之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...
- python读取配置文件configparser模块
python使用自带的configparser模块用来读取配置文件,配置文件的形式类似windows中的ini文件 目录 1.新建配置文件 2.读取配置文件 3.打印读取内容 1.新建配置文件 con ...
- Python 常用函数 configparser模块
使用ConfigParser模块读写ini文件 ConfigParserPython的ConfigParser Module中定义了3个类对INI文件进行操作.分别是RawConfigParser.C ...
- Python 配置文件之ConfigParser模块(实例、封装)
python3与python2使用configparser的区别 import configparser #python3中为configparser import ConfigParser #py ...
- python教程:ConfigParser模块使用教程
1.简介 我们经常需要使用配置文件,例如.conf和.ini等类型,使用ConfigPaser模块可以对配置文件进行操作. 2.示例 现有配置文件test.ini,其内容如下: [section_a] ...
- python中configparser详解_Python中的ConfigParser模块使用详解
1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...
- python自动加载配置文件中模块名_python----读取配置文件(configparser模块)
一.configparser模块 在工作中,常常需要把小脚本共享给其他人用.他人在使用的时候,查看修改源码不太方便.于是想到使用python中的configparser模块,只需要修改配置文件就可以运 ...
- Python 内置模块之 ConfigParser - 解析 ini 文件
ini配置文件是被configParser直接解析然后再加载的,如果只是修改配置文件,并不会改变已经加载的配置 INI文件结构简单描述 INI文件就是扩展名为"ini"的文件.在W ...
- 小白的Python之路 day5 configparser模块的特点和用法
configparser模块的特点和用法 一.概述 主要用于生成和修改常见配置文件,当前模块的名称在 python 3.x 版本中变更为 configparser.在python2.x版本中为Conf ...
最新文章
- 插件推荐(持续更新)
- 重写Java Object类中的equals()方法
- swiper动态加载数据左右切换失效
- 获取进程CPU占用率
- 2021技术文大盘点 | 打包过去,​面向未来
- SpringCloud观后感
- 应急响应— 操作系统分析(病毒、后门)
- OpenGLCG技术之Render To Texture
- Android自动化批量图片压缩插件McImage
- Android ELF文件编译之符号隐藏
- Python 构建 Random Forest 和 XGBoost
- 笔记本外接显示器 显示不清晰
- SLF4J: Class path contains multiple SLF4J bindings(log4j与logback冲突了)
- linux 将新密码邮件发送,linux脚本实现自动发送和收取邮件的设置方法
- STM32+DAC8830驱动程序
- I + I2C = I3C:这个附加的“I”是什么?
- hanlp中文语言处理--词典加载源码过程分析及自定义用户词汇添加
- 使用CSS更改鼠标(光标的样式)
- 百一测评网站切屏检测绕过
- html表格不随字数变化,设置table中的宽度不随文字改变让其固定