写项目肯定用的到配置文件,这次学习一下python中的配置文件模块 ConfigParser

安装就不说了,pip一下即可,直接来个实例

配置文件 project.conf

  1. [db]
  2. host = '127.0.0.1'
  3. port = 3306
  4. user = 'root'
  5. password = 'redhat'
  6. db = 'project'
  7.  
  8. [app]
  9. threads = 4
  10. cache_size = 8M
  11.  
  12. [game]
  13. speed = 100

脚本 project.py

  1. #!/usr/bin/python
  2. #coding: utf-8
  3.  
  4. import ConfigParser
  5.  
  6. cf = ConfigParser.ConfigParser()
  7. cf.read('project.conf')
  8.  
  9. #所有section
  10. print cf.sections()
  11.  
  12. #显示某个sections下的选项
  13. print cf.options('db')
  14.  
  15. #键值方式显示section的值
  16. print cf.items('db')
  17.  
  18. #获得某个section的某个选项的值
  19. host = cf.get('db', 'host')
  20. print host
  21.  
  22. ##get获得是string格式的,下面获得Int格式的和float格式的
  23. port = cf.getint('db', 'port')
  24. print port, type(port)
  25.  
  26. port = cf.getfloat('db', 'port')
  27. print port, type(port)
  28.  
  29. ##获得boolean方式的值
  30. play = cf.getboolean('game', 'play')
  31. print play
  32.  
  33. ##添加section
  34. cf.add_section('redis')
  35. print cf.sections()
  36.  
  37. ##添加option
  38. cf.set('redis', 'host', '127.0.0.1')
  39. print cf.items('redis')
  40.  
  41. ##修改option
  42. cf.set('db', 'host', '192.168.1.1')
  43. print cf.items('db')
  44.  
  45. ##保存配置文件
  46. cf.write(open('project.conf', 'w'))

总结方法:

  1. read(filename) #读取配置文件
  2. sections() #返回所有section
  3. options(section) #返回section中的option
  4. items(section) #返回sectiond的键值对
  5. get(section, option) #返回某个section,某个option的值,类型是string
  6. getint, getfloat, getboolean 等等返回的只是类型不同
  7.  
  8. 修改配置
  9. add_section(section) #添加section
  10. set(section,option,value) #添加或者修改值
  11. write(open(filename,'w')) #保存到配置文件

Python之配置文件模块 ConfigParser的更多相关文章

  1. Python之配置模块ConfigParser

    http://docs.python.org/2/library/configparser.html http://www.cnblogs.com/sislcb/archive/2008/11/25/ ...

  2. Python解析配置文件模块:ConfigPhaser

    算是前几周落下的博客补一篇.介绍一下python中如何解析配置文件.配置文件常用的几种格式:xml,json,还有ini.其中ini算是最简单的一种格式,因为小,解析的速度也要比xml和json快(并 ...

  3. python之常用模块ConfigParser

    这个常见于.conf,.ini等类型的配置文件 下面先看一下如果通过python生成一个.ini文件 import configparser #2.x is ConfigParserconfig = ...

  4. python之读取配置文件模块configparser(一)基本操作

    configparser模块是读取类ini文件使用,其有固定的读取格式如下: [section1] option11 = value11 option12 = value12 .... [sectio ...

  5. python之读取配置文件模块configparser(二)参数详解

    configparser.ConfigParser参数详解 从configparser的__ini__中可以看到有如下参数: def __init__(self, defaults=None, dic ...

  6. python之读取配置文件模块configparser(三)高级使用---非标准配置文件解析

    非标准配置文件也是经常使用的,如何使用configparser来解析? 这要从configparser本身解析结构来说,configparser包含section和option,非标准配置文件只有op ...

  7. python 读取配置文件ini ---ConfigParser

    Python读取ini文件需要用到 ConfigParser 模块 关于ConfigParser模块的介绍详情请参照官网解释:https://docs.python.org/2.7/library/c ...

  8. Python读写配置文件模块--Configobj

    一.介绍 我们在项目的开发过程中应该会遇到这样的问题:我们的项目读取某个配置文件,然后才能按照配置的信息正常运行服务,当我们需要对修改服务的某些信息时,可以直接修改这个配置文件,重启服务即可,不用再去 ...

  9. Python(2.7.6) ConfigParser - 读写配置文件

    Python 标准库的 ConfigParser 模块提供一套 API 来读取和操作配置文件. 配置文件的格式 a) 配置文件中包含一个或多个 section, 每个 section 有自己的 opt ...

随机推荐

  1. Javascript 判断一个数字是否含有小数点

    JavaScript 判断一个数字是否含有小数点,如果含有,则返回该数字:如果不含小数点,则小数点后保留两位有效数字: function hasDot(num){ if(!isNaN(num)){ r ...

  2. mysql IN 比等价的OR写法效率更高

  3. 理解OAuth 2.0[摘]

    原文地址:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到 ...

  4. Capabilities and Limitations of Optimizing Compilers

    Computer Systems A Programmer's Perspective Second Edition #include <stdio.h> main(){ int wr; ...

  5. Natural Language Toolkit

    http://www.nltk.org/ >>> import nltk >>> nltk.download()

  6. heapsort

    Introduction to Algorithms Third Edition The (binary) heap data structure is an array object that we ...

  7. 五 mybatis的SqlMapConfig.xml详解

    SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE conf ...

  8. J2msi 自己制作的把exe打成安装包简易GUI程序(第二版 带DLL注册)

    J2msi 自己制作的把exe打成安装包简易GUI程序(第二版 带DLL注册) 之前那一版本(http://www.cnblogs.com/rojas/p/4794684.html)没考虑 DLL 注 ...

  9. 使用JetBrains dotMemory 4.0分析内存

    安装下载地址:http://www.jetbrains.com/profiler/ 1.在本地启动web应用后,打开dotMemory,附加进程 2.附加后会看到集中颜色得粗条,不断往左边走动,这是内 ...

  10. gtd好文两篇收藏

    http://www.jianshu.com/p/bf5e8a9761f5 http://blog.sina.com.cn/s/blog_4e0f66b80100m73i.html