1.configparser模块

此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。

来看一个好多软件的常见配置文件格式如下

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes [bitbucket.org]
User = hg [topsecret.server.com]
Port = 50022
ForwardX11 = no

2、解析配置文件

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

  

  

3.其它增删改查语法

[group1]
k1 = v1
k2:v2 [group2]
k1 = v1 import ConfigParser config = 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')

# ########## 改写 ##########
sec = config.remove_section('group1')
config.write(open('i.cfg', "w")) sec = config.has_section('wupeiqi')
sec = config.add_section('wupeiqi')
config.write(open('i.cfg', "w")) config.set('group2','k1',11111) #修改选项
config.write(open('i.cfg', "w")) config.remove_option('group2','age') #移除选项,写入新文件
config.write(open('i.cfg', "w"))

  (1)修改

  (2)remove_option 删除选项

  

  (3)remove_section 删除节点

 

  

21-[模块]-configparser的更多相关文章

  1. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  2. Python之配置文件模块 ConfigParser

    写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser 安装就不说了,pip一下即可,直接来个实例 配置文件 project.conf [db] host = ' ...

  3. python学习之路-7 模块configparser/xml/shutil/subprocess以及面向对象初级入门

    本篇记录内容 模块 configparser xml shutil subprocess 面向对象 面向对象基础 面向对象编程和函数式编程对比 面向对象中对象和类的关系 面向对象之构造方法 面向对象之 ...

  4. os模块,os.path模块,subprocess模块,configparser模块,shutil模块

    1.os模块 os表示操作系统该模块主要用来处理与操作系统相关的操作最常用的文件操作打开 读入 写入 删除 复制 重命名 os.getcwd() 获取当前执行文件所在的文件夹路径os.chdir(&q ...

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

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

  6. python day 9: xlm模块,configparser模块,shutil模块,subprocess模块,logging模块,迭代器与生成器,反射

    目录 python day 9 1. xml模块 1.1 初识xml 1.2 遍历xml文档的指定节点 1.3 通过python手工创建xml文档 1.4 创建节点的两种方式 1.5 总结 2. co ...

  7. python解析模块(ConfigParser)使用方法

    python解析模块(ConfigParser)使用方法 很多软件都有配置文件,今天介绍一下python ConfigParser模块解析配置文件的使用方法 测试配置文件test.conf内容如下: ...

  8. python基础-7.3模块 configparser logging subprocess os.system shutil

    1. configparser模块 configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 继承至2版本 ConfigParser,实现了更多智能特征,实现更有可预见性,新 ...

  9. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  10. python_day7【模块configparser、XML、requests、shutil、系统命令-面向对象】之篇

    python内置模块补充 一.configparser configparser:用户处理特定格式的文件,其本质是利用open打开文件 # 节点 [section1] #键值对k1 = v1 k2:v ...

随机推荐

  1. FileTable的创建和使用

    1. 首先要在实例级别启用FILESTREAM,打开Sql Server Configuration Manager窗口,双击需要设置的SQL SERVER实例进行设置. 2. 需要更改 SQL SE ...

  2. 我们是如何拿下Google和Facebook Offer的?

    http://posts.careerengine.us/p/57c3a1c1a09633ee7e57803c 大家好,我是小高,CMU CS Master,来Offer第一期学员,2014年初在孙老 ...

  3. Linux 系统的磁盘分区_【all】

    磁盘的存储逻辑结构 1.主引导扇区(446+64+2) MBR(主引导记录)0磁头0磁道的第一扇区 446字节 -->存放系统的引导程序,同Windows 剩下的64字节,分区表(每个分区16字 ...

  4. [转]Centos7下面配置静态IP

    修改网卡配置文件(操作前先备份一下该文件),/etc/sysconfig/network-scripts/ifcfg-enp0s3 ,如下: TYPE=Ethernet BOOTPROTO=stati ...

  5. QuickBI助你成为分析师-数据建模(二)

    摘要: 数据集编辑功能界面介绍以及常见问题总结. 在数据集编辑界面可以进行数据建模来更好的展示数据,创建数据集默认将数值类型字段作为度量,日期.字符串等类型作为维度,度量可以根据维度分组展示.下面来介 ...

  6. UNIX日期与时间

    日期和时间 UINX系统内部有一个变量记录自开机以来经过的时间.从用户的角度,UNIX时间函数分为3类: 度量进程已使用CPU时间的函数: 给出绝对时间或日历时间的函数: 设置闹钟.定时器以及睡眠的函 ...

  7. Beanstalkd 的理解

    Beanstalkd 的理解 Beanstalkd 是一个轻量级的内存型队列,利用了和Memcache 类似的协议.其官网beanstakkd官网 下方的感谢语说: Many thanks to me ...

  8. November 23rd 2016 Week 48th Wednesday

    I always like walking in the rain, so no one can see me crying. 我一直喜欢在雨中行走,那样就没人能看到我的眼泪. I like walk ...

  9. November 9th 2016 Week 46th Wednesday

    Love is the poetry of the scenes. 爱是感官之诗. Recently I always feel lonely, I badly hope that I can fin ...

  10. D:\hunting2014\小题目\字符串倒序

    #include<stdio.h>#include<string.h> char *revert(char *str){ char temp; char *p = str; c ...