1,函数介绍

1.1.读取配置文件

-read(filename) 直接读取ini文件内容
-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到该section的所有option
-items(section) 得到该section的所有键值对
-get(section,option) 得到section中option的值,返回为string类型
-getint(section,option) 得到section中option的值,返回为int类型

1.2.写入配置文件

-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置
  需要调用write将内容写入配置文件。

2,测试实例

2.1,测试1

配置文件test.cfg

  1. [sec_a]
  2. a_key1 = 20
  3. a_key2 = 10
  4. [sec_b]
  5. b_key1 = 121
  6. b_key2 = b_value2
  7. b_key3 = $r
  8. b_key4 = 127.0.0.1

测试文件test.py

  1. # -* - coding: UTF-8 -* -
  2. import ConfigParser
  3. #生成config对象
  4. conf = ConfigParser.ConfigParser()
  5. #用config对象读取配置文件
  6. conf.read("test.cfg")
  7. #以列表形式返回所有的section
  8. sections = conf.sections()
  9. print 'sections:', sections         #sections: ['sec_b', 'sec_a']
  10. #得到指定section的所有option
  11. options = conf.options("sec_a")
  12. print 'options:', options           #options: ['a_key1', 'a_key2']
  13. #得到指定section的所有键值对
  14. kvs = conf.items("sec_a")
  15. print 'sec_a:', kvs                 #sec_a: [('a_key1', '20'), ('a_key2', '10')]
  16. #指定section,option读取值
  17. str_val = conf.get("sec_a", "a_key1")
  18. int_val = conf.getint("sec_a", "a_key2")
  19. print "value for sec_a's a_key1:", str_val   #value for sec_a's a_key1: 20
  20. print "value for sec_a's a_key2:", int_val   #value for sec_a's a_key2: 10
  21. #写配置文件
  22. #更新指定section,option的值
  23. conf.set("sec_b", "b_key3", "new-$r")
  24. #写入指定section增加新option和值
  25. conf.set("sec_b", "b_newkey", "new-value")
  26. #增加新的section
  27. conf.add_section('a_new_section')
  28. conf.set('a_new_section', 'new_key', 'new_value')
  29. #写回配置文件
  30. conf.write(open("test.cfg", "w"))

2.2,测试2

配置文件test.cfg
  1. [info]
  2. age = 21
  3. name = chen
  4. sex = male

测试文件test.py

  1. from __future__ import with_statement
  2. import ConfigParser
  3. config=ConfigParser.ConfigParser()
  4. with open("test.cfg","rw") as cfgfile:
  5. config.readfp(cfgfile)
  6. name=config.get("info","name")
  7. age=config.get("info","age")
  8. print name
  9. print age
  10. config.set("info","sex","male")
  11. config.set("info","age","55")
  12. age=config.getint("info","age")
  13. print name
  14. print type(age)
  15. print age

分析

其中[ ] 中的info是这段配置的名字。

其中age,name都是属性。

首先,config=ConfigParser.ConfigParser() 得到一个配置config对象.下面打开一个配置文件 cfgfile. 用readfp()读取这个文件.这样配置的内容就读到config对象里面了。

接下来一个问题是如何读取值.常用的方法是get() 和getint() . get()返回文本. getint()返回整数。

其次,name=config.get(''info'',''name'')  意思就是.读取config中info段中的name变量值。

最后讲讲如何设置值.使用set(段名,变量名,值) 来设置变量.config.set(''info'',''age'',''21'') 表示把info段中age变量设置为21。

2.3,测试3

Python的ConfigParser Module中定义了3个类对INI文件进行操作。

分别是RawConfigParser、ConfigParser、SafeConfigParser。

RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。

配置文件test.cfg

  1. [portal]
  2. url = http://%(host)s:%(port)s/Portal
  3. host = localhost
  4. port = 8080

使用RawConfigParser:

  1. import ConfigParser
  2. conf = ConfigParser.RawConfigParser()
  3. print "use RawConfigParser() read"
  4. conf.read("test.cfg")
  5. print conf.get("portal", "url")
  6. print "use RawConfigParser() write"
  7. conf.set("portal", "url2", "%(host)s:%(port)s")
  8. print conf.get("portal", "url2")

得到输出

  1. use RawConfigParser() read
  2. http://%(host)s:%(port)s/Portal
  3. use RawConfigParser() write
  4. %(host)s:%(port)s

改用ConfigParser

  1. import ConfigParser
  2. conf = ConfigParser.ConfigParser()
  3. print "use RawConfigParser() read"
  4. conf.read("test.cfg")
  5. print conf.get("portal", "url")
  6. print "use RawConfigParser() write"
  7. conf.set("portal", "url2", "%(host)s:%(port)s")
  8. print conf.get("portal", "url2")

得到输出

  1. use RawConfigParser() read
  2. http://localhost:8080/Portal
  3. use RawConfigParser() write
  4. localhost:8080

改用SafeConfigParser,效果与ConfigParser相同

  1. import ConfigParser
  2. conf = ConfigParser.SafeConfigParser()
  3. print "use RawConfigParser() read"
  4. conf.read("test.cfg")
  5. print conf.get("portal", "url")
  6. print "use RawConfigParser() write"
  7. conf.set("portal", "url2", "%(host)s:%(port)s")
  8. print conf.get("portal", "url2")

结论:

还是用ConfigParser

python-ConfigParser模块--转载的更多相关文章

  1. Python Configparser模块读取、写入配置文件

    写代码中需要用到读取配置,最近在写python,记录一下. 如下,假设有这样的配置. [db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= ...

  2. python -ConfigParser模块讲解

    configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...

  3. python ConfigParser模块 配置文件解析

    ConfigParser模块主要是用来解析配置文件的模块,像mysql,或者win下面的ini文件等等 下面我们来解析mysql的配置文件my.cnf my.cnf配置文件内容 [mysqld] da ...

  4. 【python】python configparser模块

    ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section), 每个节可以有多个参数(键=值).使用的配置 ...

  5. python configparser模块详解

    此模块提供了一个实现基本配置语言的类 首先来看一个非常基本的配置文件,如下所示格式: [DEFAULT] ServerAliveInterval = 45 Compression = yes Comp ...

  6. Python - configParser模块学习

    configParser 模块用于操作配置文件 注:Parser汉译为“解析”之意. 配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键= ...

  7. 聊聊Python ctypes 模块(转载)

    https://zhuanlan.zhihu.com/p/20152309?columnSlug=python-dev 作者:Jerry Jho链接:https://zhuanlan.zhihu.co ...

  8. python ConfigParser 模块

    ConfigParser的函数方法 读取配置文件 read(filename) 直接读取ini文件内容 sections() 得到所有的section,并以列表的形式返回 options(sectio ...

  9. python configparser模块

    来看一个好多软件的常见文档格式如下: [DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 Forward ...

  10. Python configparser模块操作代码实例

    1.生成配置文件 ''' 生成配置文件 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知 ...

随机推荐

  1. IDA 7.0在Mojava更新后打不开的问题

    Mac升级到mojava后,ida 7.0打不开了. 上述是两种典型的窗口,不过不论是出现什么样的弹窗,如果是在升级之后出现的,都要试一下下面的解决办法.因为IDA7.0版本流出的比较多,虽然这个已经 ...

  2. Java-二叉树算法

    二叉树算法的排序规则: 1.选择第一个元素作为根节点 2.之后如果元素大于根节点放在右子树,如果元素小于根节点,则放在左子树 3.最后按照中序遍历的方式进行输出,则可以得到排序的结果(左->根- ...

  3. testng入门教程2用TestNG编写测试及执行测试

    编写TestNG测试基本上包括以下步骤: 测试和编写业务逻辑,在代码中插入TestNG的注解.. 添加一个testng.xml文件或build.xml中在测试信息(例如类名,您想要运行的组,等..) ...

  4. iOS UI基础-4.2应用程序管理 Xib文件使用

    Xib调整使用 1.新建xib文件 New File-->User Interface-->Empty 2.打开新建的xib文件,出现可视化窗口 (1)拖入一个UIView (不是UIVi ...

  5. Lintcode: Find Peak Element

    There is an integer array which has the following features: * The numbers in adjacent positions are ...

  6. centos迷你版,没有安装ifconfig命令

    ifconfig命令是设置或显示网络接口的程序,可以显示出我们机器的网卡信息,可是有些时候最小化安装CentOS等Linux发行版的时候会默认不安装ifconfig等命令,这时候你进入终端,运行ifc ...

  7. volatile的语义与实现

    1.volatile关键字的两层语义 一旦一个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: 1)保证了不同线程对这个变量进行操作时的可见性,即一个线程修 ...

  8. git提交时候出错

    Please make sure you have the correct access rights and the repository exists. 解决方案: 主要原因是没有加载keygen ...

  9. js的Base64编码与解码

    js的Base64编码与解码 pc和手机app项目中,经常需要将手机自带的表情图片转换特定的编码格式与后台进行交互. Base64其实是一种简单的置换加密方式,但是BASE64的用处往往并不是为了防止 ...

  10. Ignite初探

    Guava是一个很方便的本地缓存工具,但是在多节点处理的过程中,本地缓存无法满足数据一致性的问题.分布式缓存Ignite很好的解决了数据一致性,可靠性,事务性等方面的问题. Ignite支持分区方式和 ...