解析配置文件ConfigParser模块

转自:http://blog.csdn.net/cindy9902/article/details/7915743

配置文件内容:

[plain]  view plain copy
  1. [db]  
  2. db_host=127.0.0.1  
  3. db_port=3306  
  4. db_user=root  
  5. db_pass=password  
  6. [concurrent]  
  7. thread=10  
  8. processor=20  

如果遵循以上格式,那么就可以用python的ConfigParser模块。

[python]  view plain copy
  1. #!/usr/bin/python  
  2. # Filename: parse_module.py  
  3.   
  4. import ConfigParser  
  5.   
  6. filename = 'my.conf'  
  7.   
  8. parse = ConfigParser.ConfigParser()  
  9.   
  10. parse.read(filename)  
  11.   
  12. sections = parse.sections()  
  13.   
  14. for i in sections:  
  15.     options = parse.options(i)  
  16.     values = parse.items(i)  
  17.     print "Section is %s " % i  
  18.     print "Options are %s " % options  
  19.     print "Values are %s " % values  

输出:

[plain]  view plain copy
  1. Section is db   
  2. Options are ['db_host', 'db_port', 'db_user', 'db_pass']   
  3. Values are [('db_host', '127.0.0.1'), ('db_port', '3306'), ('db_user', 'root'), ('db_pass', 'password')]   
  4. Section is concurrent   
  5. Options are ['thread', 'processor']   
  6. Values are [('thread', '10'), ('processor', '20')]   

但是如果配置文件中,parameter选项中含有[],则无法用此模块进行解析。。因此当试用python作为开发语言时,配置文件的格式可以选择符合configparser模块的格式。。。

下面的配置就无法使用configparser模块. 会将含有[]的部分当作section来解析。。。。

[plain]  view plain copy
  1. [db]  
  2. host[0]=127.0.0.1  
  3. host[1] = 192.168.1.123  
  4. db_port=3306  
  5. db_user=root  
  6. db_pass=password  
  7. [concurrent]  
  8. thread=10  
  9. processor=20  


使用configparser模块解析配置文件时,发现的问题:

  1. 参数名称的大写全部会转换为小写。
  2. 参数名称不能含有[,]
  3. 如果含有多个名字相同的section时,会以最后一个section为准。


一个简单的读配置文件的代码,其中section部分相当与忽略了,而返回一个包含所有的参数的dict。

[python]  view plain copy
  1. #!/usr/bin/python  
  2. # Filename: readConf.py  
  3.   
  4. import ConfigParser  
  5.   
  6. cofile = 'test2.properties'  
  7.   
  8. def read_config_file(filename):  
  9.     '''''Read_config_file(filename) 
  10.  
  11.         this function is used for parse the config file'''      
  12.     data = {}  
  13.     config = ConfigParser.ConfigParser()  
  14.     try:  
  15.         with open(filename,'r') as confile:  
  16.             config.readfp(confile)  
  17.         #config.read(filename)  
  18.             for i in config.sections():  
  19.                 for (key, value) in config.items(i):  
  20.                     data[key] = value  
  21.             return data      
  22.     except Exception:  
  23.         print "Open file error."  
  24.   
  25. p = read_config_file(cofile)  

如果有更简单或者更清晰的代码,欢迎大家指正。。。。。


还是发现了一个问题,如果不同的section中含有相同参数名字的参数,那个上面这个解析将无法判断出参数来自哪个section。

例如,myslq的配置文件中,client section和mysqld section中都含有socket和port这两个参数。如果用上面的代码,则无法判断哪个dict中的两个socket和port分别来自哪个section。。。


这样存放了之后,dict中的元素的先后顺序,和添加的顺序有不同。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值