zoukankan      html  css  js  c++  java
  • xls7-python读conf配置文件--ConfigParser

    xls7-python读conf配置文件--ConfigParser

     

    分类: Python/Ruby

    2015-02-04 17:35:31

     
    python读写配置文件还是比较方便得。
     
    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类型,还有相应的getboolean()和getfloat() 函数。
     
    2) 基本的写入配置文件
         -add_section(section) 添加一个新的section
         -set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
      
    配置文件如下:

    点击(此处)折叠或打开

    1. [db]
    2. db_port = 3306
    3. db_user = root
    4. db_host = 127.0.0.1
    5. db_pass = xgmtest
    6.  
    7. [concurrent]
    8. processor = 20
    9. thread = 10
     
    示例代码如下

    点击(此处)折叠或打开

    1. #/usr/bin/python
    2.  
    3. import ConfigParser
    4. import string, os, sys
    5.  
    6. cf = ConfigParser.ConfigParser()
    7.  
    8. cf.read("test.conf")
    9.  
    10. #return all section
    11. secs = cf.sections()
    12. print 'sections:', secs
    13.  
    14. opts = cf.options("db")
    15. print 'options:', opts
    16.  
    17. kvs = cf.items("db")
    18. print 'db:', kvs
    19.  
    20. #read by type
    21. db_host = cf.get("db", "db_host")
    22. db_port = cf.getint("db", "db_port")
    23. db_user = cf.get("db", "db_user")
    24. db_pass = cf.get("db", "db_pass")
    25.  
    26. #read int
    27. threads = cf.getint("concurrent", "thread")
    28. processors = cf.getint("concurrent", "processor")
    29.  
    30. print "db_host:", db_host
    31. print "db_port:", db_port
    32. print "db_user:", db_user
    33. print "db_pass:", db_pass
    34.  
    35. print "thread:", threads
    36. print "processor:", processors
    37.  
    38.  
    39. #modify one value and write to file
    40. cf.set("db", "db_pass", "xgmtest")
    41. cf.write(open("test.conf", "w"))
      
    3) Python的ConfigParser Module 中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。 RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的 解析。
     
    设定配置文件test2.conf

    点击(此处)折叠或打开

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

    使用RawConfigParser:

    点击(此处)折叠或打开

    1. import ConfigParser
    2. cf = ConfigParser.RawConfigParser()
    3. print "use RawConfigParser() read"
    4. cf.read("test2.conf")
    5. print cf.get("portal", "url")
    6. print "use RawConfigParser() write"
    7. cf.set("portal", "url2", "%(host)s:%(port)s")
    8. print cf.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. cf = ConfigParser.ConfigParser()
    3. print "use ConfigParser() read"
    4. cf.read("test2.conf")
    5. print cf.get("portal", "url")
    6. print "use ConfigParser() write"
    7. cf.set("portal", "url2", "%(host)s:%(port)s")
    8. print cf.get("portal", "url2")

    得到终端输出:

    点击(此处)折叠或打开

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

    改用SafeConfigParser:

    点击(此处)折叠或打开

    1. import ConfigParser
    2. cf = ConfigParser.SafeConfigParser()
    3. print "use SafeConfigParser() read"
    4. cf.read("test2.conf")
    5. print cf.get("portal", "url")
    6. print "use SateConfigParser() write"
    7. cf.set("portal", "url2", "%(host)s:%(port)s")
    8. print cf.get("portal", "url2")
     
    得到终端输出(效果同ConfigParser):

    点击(此处)折叠或打开

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


     
  • 相关阅读:
    转载文章----.NET 框架浅析
    转载文章----IL反编译利器——Ildasm.exe和Reflector.exe:
    转载文章----初识Ildasm.exe——IL反编译的实用工具
    北航物理实验
    计算机组成原理往年试题以及答案(tzf!!!)
    Java 往年试卷参考答案!!!
    算法导论期末考试英文练习题带答案!!!
    算法导论( FFT & 自动机 & 最优二叉搜索树 !!!)
    Java部分总结图片版2(已加上原图链接!!!)
    Java部分总结图片版(已经加上原图链接下载!!!)
  • 原文地址:https://www.cnblogs.com/xinxihua/p/12642813.html
Copyright © 2011-2022 走看看