ssplyh 发表于 2018-8-7 13:43:59

【python】 ConfigParser模块

  在java中我们会有properties属性文件,在python也有一种类似的属性配置文件,该文件对文件类型没有太大要求,但是文件的内容格式如下:
  
username=admin
  
password=wangzp
  
email=1212121@qq.com
  

  

  
friend1=wangzp
  
friend2=tanhq
  下面将通过python来读取上述的信息,在此之前,介绍几个概念:
  1、section :区域,也就是"[]"内的值;
  2、option : 选项,即就是key=value形式的键值对
  一个属性文件,可以包含多个区域;一个区域包含多个选项;一个选项由key和value构成。
  下面列子将通过python读取上述文件信息。
  一、读取属性文件
# -*- coding:utf-8 -*-  
"""
  
      读取属性文件
  
"""
  
from ConfigParser import ConfigParser
  

  
# 创建ConfigParser对象
  
config = ConfigParser()
  

  
# 读取属性配置文件(config.ini)
  
config.read("config.ini")# 注:文件可以是绝对路径,也可以是相对路径
  

  
# 获取属性文件的所有区域
  
sections = config.sections()
  
print("=============获取所有区域==============")
  
print(sections)
  

  
# 获取属性文件中某个区域中的所有选项
  
print("=============获取所有选项==============")
  
for section in sections:
  
    options = config.options(section)
  
    print(options)
  

  
# 获取指定区域的特定选项
  
# 例如:获取user区域的username选项
  
username = config.get("user", "username")
  
print("=============获取user区域的username选项==============")
  
print(username)
  

  
# 遍历所有区域的所有项
  
print("=============遍历所有区域的所有项==============")
  
for section in sections:
  
    print("************ " + section + " *************")
  
    items = config.items(section)
  
    for (key, value) in items:
  
      print("%s : %s" % (key, value))
  运行结果:
=============获取所有区域==============  
['user', 'friend']
  
=============获取所有选项==============
  
['username', 'password', 'email']
  
['friend1', 'friend2']
  
=============获取user区域的username选项==============
  
admin
  
=============遍历所有区域的所有项==============
  
************ user *************
  
username : admin
  
password : wangzp
  
email : 1212121@qq.com
  
************ friend *************
  
friend1 : wangzp
  
friend2 : tanhq
  二、写入属性/添加属性
# -*- coding:utf-8 -*-  
"""
  
    写入属性文件,写入上述读取的文件
  
"""
  
from ConfigParser import ConfigParser
  

  
# 构造属性对象
  
config = ConfigParser()
  

  
# 添加区域
  
config.add_section("user")
  
config.add_section("friend")
  

  
# 设置属性值
  
config.set("user", "username", "admin")
  
config.set("user", "password", "admin")
  
config.set("user", "email", "1212121@qq.com")
  

  
config.set("friend", "friend1", "wangzp")
  
config.set("friend", "friend2", "tanhq")
  

  
# 创建属性文件
  
file = open("writeConfig.ini", 'w')
  
config.write(file)
  

  
# 添加属性
  
appendFile = open('writeConfig.ini', 'a')
  
config.add_section('append')
  
config.set('append', 'append_info', 'append success')
  
config.write(appendFile)
  执行结果:
  
username = admin
  
password = admin
  
email = 1212121@qq.com
  

  

  
friend1 = wangzp
  
friend2 = tanhq
  

  

  
append_info = append success
  三、部分方法解释
  1、ConfigParser():构造一个属性对象;
  2、ConfigParser.read(filename):读取文件,filename是属性文件;
  3、ConfigParser.sections():获取所有的区域,也就是中括号内容;
  4、ConfigParser.options(section): 获取某个区域下的所有选项(Key);
  5、ConfigParser.items(section):获取某个区域下的所有选项(key和value);
  6、ConfigParser.get(section, option):获取某个区域下指定的选项值;
  7、ConfigParser.write(fileObj):写入一个文件对象,依据文件对象的方式确定是覆盖写入还是添加,即fileObj = open(filename, 'w')或者fileObj = open(filename, 'a');
  8、ConfigParser.add_section(section):写入属性操作时,添加区域,必须现写入区域,然后再写入选项,否则会报错区域不存在;
  9、ConfigParser.set(section, option, value):写入某个区域的某个选项(key-value)
  10、还有一些诸如ConfigParser.has_section(section)、ConfigParser.has_option(section, option)判断是否存在某些区域或者选项的函数,这里就不做介绍了。
  注:ConfigParser模块,在2.7中命名为ConfigParser,在3.x中命名为configparser
页: [1]
查看完整版本: 【python】 ConfigParser模块