此模块用于生成和修改常见配置文档,当前模块的名称在 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.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' 

其它增删改查语法

[group1]
k1 = v1
k2:v2[group2]
k1 = v1import ConfigParserconfig = ConfigParser.ConfigParser()
config.read('i.cfg')# ########## 读 ##########
#secs = config.sections()
#print secs
#options = config.options('group2')
#print options#item_list = config.items('group2')
#print item_list#val = config.get('group1','key')
#val = config.getint('group1','key')

 

# ########## 改写 ##########
import configparserconfig = configparser.ConfigParser()config.read('test.ini')

# sec = config.remove_section('group1')# config.write(open('i.cfg', 'w'))

# sec = config.has_section('wupeiqi')# print(sec)# sec = config.add_section('wupeiqi')# config.write(open('i.cfg', 'w'))

# config.set('group2', 'k1', '111111')# config.write(open('i.cfg', 'w'))

config.remove_option('group2', 'k3')config.write(open('i.cfg', 'w'))

转载于:https://www.cnblogs.com/xudachen/p/8490932.html

Python基础-ConfigParser模块相关推荐

  1. python基础——使用模块

    python基础--使用模块 Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env ...

  2. python基础之模块之os模块

    python基础之模块之os模块 os模块 os模块的作用: os,语义为操作系统,所以肯定就是操作系统相关的功能了,可以处理文件和目录这些我们日常手动需要做的操作,就比如说:显示当前目录下所有文件/ ...

  3. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

  4. 用于生成随机数的python标准库模块是_详解Python基础random模块随机数的生成

    详解Python基础random模块随机数的生成 来源:中文源码网    浏览: 次    日期:2019年11月5日 [下载文档:  详解Python基础random模块随机数的生成.txt ] ( ...

  5. python random库生成伯努利随机数的方法_详解Python基础random模块随机数的生成

    随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等.Python内置的random模块提供了生成随机数的方法,使用这些方法时需要导入ran ...

  6. 【Python的configparser模块读取.ini文件内容并输出】

    configparser是 Pyhton 标准库中用来解析配置文件的模块,并且内置方法和字典非常接近.Python2.x 中名为 ConfigParser,3.x 已更名小写.下文通过使用python ...

  7. Python基础之模块和包

    Python基础之模块和包 本节将介绍Python中的模块和包的概念及基本用法. 模块 简单来说模块就是一个python文件,我们可以将一些常量.函数.类等封装到一个模块中,然后在程序中使用该模块.模 ...

  8. Python之configparser模块详解和使用

    1 configparser安装 pip3 install configparser 2 configparser简介 用来读取配置文件的python包: 一般做自动化测试的时候,会使用到这个模块,用 ...

  9. 刻意练习:Python基础 -- Task12. 模块

    背景 我们准备利用17天时间,将 "Python基础的刻意练习" 分为如下任务: Task01:变量.运算符与数据类型(1day) Task02:条件与循环(1day) Task0 ...

最新文章

  1. onnx onnxruntime 预测
  2. 在Eclipse中使用JUnit4进行单元测试(中级篇)
  3. 浅谈商业银行绿色数据中心建设
  4. C++虚函数与多态性
  5. Python带参数的装饰器
  6. 【转】解决win7下重装winXP 系统启动选择菜单消失 问题
  7. java默认异常处理_spring boot 默认异常处理的实现
  8. BGP安全特性(华为设备)
  9. 灵修---士师记第9章
  10. 形态学滤波对图像进行边缘及角点检测
  11. Java项目服务器cpu占用100%解决办法
  12. 收集整理的一些windows好用的工具(持续更新)
  13. 批量爬取链家房源信息
  14. Deltix宣布CryptoCortex与MPC钱包提供商Curv整合
  15. 用Mothur制作OTUtable
  16. 权力的游戏字幕哪家强_使用权力游戏字幕
  17. 软件设计师证书重要吗?
  18. 计算机操作系统——(第四章) 存储器管理/内存管理
  19. 手机进行linux编程的 app,手机也能编程?盘点这6个可以用手机编程的App!快收藏...
  20. 生产者-消费者中的缓冲区:BlockingQueue接口

热门文章

  1. ffmpeg-filter 入门
  2. 说好的人工智能 怎么只看到高科技玩具?
  3. 确保客户端可以接收到服务端的异常serviceDebug includeExceptionDetailInFaults=true
  4. (转帖)C#--web services之wsdl文件生成cs
  5. Silverlight 视频学习札记(一)
  6. 各种控制列表--前缀列表
  7. 开发人员的幸福:您需要知道的
  8. 使用DistroTweaks复制您的自定义Linux设置
  9. Dave和Gunnar采访Lauren Egts:Raspberry Pi,Scratch等
  10. Bootstrap3 模态对话框的方法