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'))

来自为知笔记(Wiz)

转载于:https://www.cnblogs.com/hetan/p/5211133.html

python小白-day6 ConfigParser模块相关推荐

  1. python:实例化configparser模块读写配置文件

    之前的博客介绍过利用python的configparser模块读写配置文件的基础用法,这篇博客,介绍下如何实例化,方便作为公共类调用. 实例化的好处有很多,既方便调用,又降低了脚本的维护成本,而且提高 ...

  2. python读取配置文件configparser模块

    python使用自带的configparser模块用来读取配置文件,配置文件的形式类似windows中的ini文件 目录 1.新建配置文件 2.读取配置文件 3.打印读取内容 1.新建配置文件 con ...

  3. Python 常用函数 configparser模块

    使用ConfigParser模块读写ini文件 ConfigParserPython的ConfigParser Module中定义了3个类对INI文件进行操作.分别是RawConfigParser.C ...

  4. Python 配置文件之ConfigParser模块(实例、封装)

    python3与python2使用configparser的区别 import configparser  #python3中为configparser import ConfigParser #py ...

  5. python教程:ConfigParser模块使用教程

    1.简介 我们经常需要使用配置文件,例如.conf和.ini等类型,使用ConfigPaser模块可以对配置文件进行操作. 2.示例 现有配置文件test.ini,其内容如下: [section_a] ...

  6. python中configparser详解_Python中的ConfigParser模块使用详解

    1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -options(section) 得到该sect ...

  7. python自动加载配置文件中模块名_python----读取配置文件(configparser模块)

    一.configparser模块 在工作中,常常需要把小脚本共享给其他人用.他人在使用的时候,查看修改源码不太方便.于是想到使用python中的configparser模块,只需要修改配置文件就可以运 ...

  8. Python 内置模块之 ConfigParser - 解析 ini 文件

    ini配置文件是被configParser直接解析然后再加载的,如果只是修改配置文件,并不会改变已经加载的配置 INI文件结构简单描述 INI文件就是扩展名为"ini"的文件.在W ...

  9. 小白的Python之路 day5 configparser模块的特点和用法

    configparser模块的特点和用法 一.概述 主要用于生成和修改常见配置文件,当前模块的名称在 python 3.x 版本中变更为 configparser.在python2.x版本中为Conf ...

最新文章

  1. 插件推荐(持续更新)
  2. 重写Java Object类中的equals()方法
  3. swiper动态加载数据左右切换失效
  4. 获取进程CPU占用率
  5. 2021技术文大盘点 | 打包过去,​面向未来
  6. SpringCloud观后感
  7. 应急响应— 操作系统分析(病毒、后门)
  8. OpenGLCG技术之Render To Texture
  9. Android自动化批量图片压缩插件McImage
  10. Android ELF文件编译之符号隐藏
  11. Python 构建 Random Forest 和 XGBoost
  12. 笔记本外接显示器 显示不清晰
  13. SLF4J: Class path contains multiple SLF4J bindings(log4j与logback冲突了)
  14. linux 将新密码邮件发送,linux脚本实现自动发送和收取邮件的设置方法
  15. STM32+DAC8830驱动程序
  16. I + I2C = I3C:这个附加的“I”是什么?
  17. hanlp中文语言处理--词典加载源码过程分析及自定义用户词汇添加
  18. 使用CSS更改鼠标(光标的样式)
  19. 百一测评网站切屏检测绕过
  20. html表格不随字数变化,设置table中的宽度不随文字改变让其固定

热门文章

  1. 通过递归算法完成树的级联勾选的一般思路
  2. iOS开发之蓝牙通信
  3. [WCF编程]2.SOA概述
  4. 设计模式学习笔记二:简单工厂模式
  5. mysql show语句_mysql常用show语句
  6. Golang 判断key是否在map中
  7. java 二分查找_计算机入门必备算法——二分查找法
  8. python字符串操作_浅谈Python 字符串特有的操作方法
  9. java同步方法完成案例_Java同步代码块和同步方法原理与应用案例详解
  10. kafka之Producer同步与异步消息发送及事务幂等性案例应用实战