configParser 模块用于操作配置文件

注:Parser汉译为“解析”之意。

配置文件的格式与windows ini文件类似,可以包含一个或多个节(section),每个节可以有多个参数(键=值)。

为了更好的理解本文,我们先了解一下配置文件的组成及命名:配置文件(INI文件)由节(section)、键、值组成。

样例配置文件example.ini

  1. [book]
  2. title:ConfigParser模块教程
  3. time:2012-09-20 22:04:55
  4. [size]
  5. size:1024
  6. [other]
  7. blog:csdn.net

上面配置文件中用的是冒号,也可以用等号。

example.py代码

  1. # -*- coding: utf-8 -*-
  2. import ConfigParser
  3. import string
  4. config=ConfigParser.ConfigParser()
  5. config.read(u'd:/百度网盘/android/Python/python_example/sample.ini')
  6. print string.upper(config.get("book","title")),
  7. print "by",config.get("book","author"),
  8. print "("+config.get("book","email")+")"
  9. print
  10. print config.get("size","size")
  11. print
  12. print config.sections()
  13. for section in config.sections():
  14. print section
  15. for option in config.options(section):
  16. print " ",option,"=",config.get(section,option)

example.py执行结果

  1. C:\Documents and Settings\Administrator>tmp.py
  2. CONFIGPARSER模块教程 by 大头爸爸 (366500050@qq.com)
  3. 1024
  4. ['book', 'size', 'other']
  5. book
  6. title = ConfigParser模块教程
  7. author = 大头爸爸
  8. email = 366500050@qq.com
  9. time = 2012-09-20 22:04:55
  10. size
  11. size = 1024
  12. other
  13. blog = csdn.net

写配置文件实例

  1. import ConfigParser
  2. import sys
  3. config=ConfigParser.ConfigParser()
  4. config.add_section("book")
  5. config.set("book","title","这是标题")
  6. config.set("book","author","大头爸爸")
  7. config.add_section("size")
  8. config.set("size","size",1024)
  9. config.write(sys.stdout)

执行结果

  1. [book]
  2. title = 这是标题
  3. author = 大头爸爸
  4. [size]
  5. size = 1024

ConfigParser方法

  1. 1、config=ConfigParser.ConfigParser()
  2. 创建ConfigParser实例
  3. 2、config.sections()
  4. 返回配置文件中节序列
  5. 3、config.options(section)
  6. 返回某个项目中的所有键的序列
  7. 4、config.get(section,option)
  8. 返回section节中,option的键值
  9. 5、config.add_section(str)
  10. 添加一个配置文件节点(str)
  11. 6、config.set(section,option,val)
  12. 设置section节点中,键名为option的值(val)
  13. 7、config.read(filename)
  14. 读取配置文件
  15. 8、config.write(obj_file)
  16. 写入配置文件

综合实例

  1. #coding=utf-8
  2. import ConfigParser
  3. def writeConfig(filename):
  4. config = ConfigParser.ConfigParser()
  5. # set db
  6. section_name = 'db'
  7. config.add_section( section_name )
  8. config.set( section_name, 'dbname', 'MySQL')
  9. config.set( section_name, 'host', '127.0.0.1')
  10. config.set( section_name, 'port', '80')
  11. config.set( section_name, 'password', '123456')
  12. config.set( section_name, 'databasename', 'test')
  13. # set app
  14. section_name = 'app'
  15. config.add_section( section_name )
  16. config.set( section_name, 'loggerapp', '192.168.20.2')
  17. config.set( section_name, 'reportapp', '192.168.20.3')
  18. # write to file
  19. config.write( open(filename, 'a') )
  20. def updateConfig(filename, section, **keyv):
  21. config = ConfigParser.ConfigParser()
  22. config.read(filename)
  23. print config.sections()
  24. for section in config.sections():
  25. print "[",section,"]"
  26. items = config.items(section)
  27. for item in items:
  28. print "\t",item[0]," = ",item[1]
  29. print config.has_option("dbname", "MySQL")
  30. print config.set("db", "dbname", "11")
  31. print "..............."
  32. for key in keyv:
  33. print "\t",key," = ", keyv[key]
  34. config.write( open(filename, 'r+') )
  35. if __name__ == '__main__':
  36. file_name = 'test.ini'
  37. writeConfig(file_name)
  38. updateConfig(file_name, 'app', reportapp = '192.168.100.100')
  39. print "end__"

python -ConfigParser模块讲解的更多相关文章

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

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

  2. PyYAML和configparser模块讲解

    Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation ymal主要用于配置文件. Co ...

  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 ConfigParser 模块

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

  8. python configparser模块

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

  9. python itertools 模块讲解

    1.介绍itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存. 使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环. - 无限迭代器 itertool ...

随机推荐

  1. unity 小地图的制作

    利用 Transform.InverseTransformDirection  变换位置从世界坐标到自身坐标. 以第一人称控制器为坐标原点(忽视y轴),x轴z轴转为屏幕坐标. 若物体在地图范围外,可以 ...

  2. 编写高质量代码改善C#程序的157个建议——建议87:区分WPF和WinForm的线程模型

    建议87:区分WPF和WinForm的线程模型 WPF和WinForm窗体应用程序都有一个要求,那就是UI元素(如Button.TextBox等)必须由创建它的那个线程进行更新.WinForm在这方面 ...

  3. 编写高质量代码改善C#程序的157个建议——建议82:Parallel简化但不等同于Task默认行为

    建议82:Parallel简化但不等同于Task默认行为 建议81说到了Parallel的使用方法,不知道大家是否注意到文中使用的字眼:在同步状态下简化了Task的使用.也就是说,在运行Paralle ...

  4. C/C++ Pthread线程

    线程按照其调度者可以分为用户级线程和核心级线程两种 用户级线程主要解决的是上下文切换的问题,它的调度算法和调度过程全部由用户自行选择决定,在运行时不需要特定的内核支持: 我们常用基本就是用户级线程,所 ...

  5. kafka (搜索) 在idea api操作(官方apihttp://kafka.apache.org/documentation/#producerapi)

     https://blog.csdn.net/isea533/article/details/73822881        这个不推荐,可以看一下(https://www.cnblogs.com/b ...

  6. Domino 迁移到Exchange 之 Domino Admin 安装!

    双击Setup 安装:

  7. ZOJ3705:Applications

    Recently, the ACM/ICPC team of Marjar University decided to choose some new members from freshmen to ...

  8. 小试maven工程

    由于工作中要用到maven来进行开发j2ee开发,所以选用了集成maven的eclipse版本: 下载地址: https://www.eclipse.org/downloads/ 根据提示下载32或者 ...

  9. [LeetCode 题解]: palindromes

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  10. Mathcad操作tips:2D绘图

    1. 直接输入算式进行绘图(QuickPlot) 2. 先定义函数,再利用函数绘制多个曲线.一张图最多支持16条曲线.留意“,”的用法. 3. 利用空格键和","在现有绘图上增加新 ...