configParser 模塊用於操作配置文件
注:Parser漢譯為“解析”之意。
配置文件的格式與windows ini文件類似,可以包含一個或多個節(section),每個節可以有多個參數(鍵=值)。
為了更好的理解本文,我們先了解一下配置文件的組成及命名:配置文件(INI文件)由節(section)、鍵、值組成。
樣例配置文件example.ini
- [book]
- title:ConfigParser模塊教程
- time:2012-09-20 22:04:55
- [size]
- size:1024
- [other]
- blog:csdn.net
上面配置文件中用的是冒號,也可以用等號。
example.py代碼
- # -*- coding: utf-8 -*-
- import ConfigParser
- import string
- config=ConfigParser.ConfigParser()
- config.read(u'd:/百度網盤/android/Python/python_example/sample.ini')
- print string.upper(config.get("book","title")),
- print "by",config.get("book","author"),
- print "("+config.get("book","email")+")"
- print config.get("size","size")
- print config.sections()
- for section in config.sections():
- print section
- for option in config.options(section):
- print " ",option,"=",config.get(section,option)
example.py執行結果
- C:\Documents and Settings\Administrator>tmp.py
- CONFIGPARSER模塊教程 by 大頭爸爸 (366500050@qq.com)
- 1024
- ['book', 'size', 'other']
- book
- title = ConfigParser模塊教程
- author = 大頭爸爸
- email = 366500050@qq.com
- time = 2012-09-20 22:04:55
- size
- size = 1024
- other
- blog = csdn.net
寫配置文件實例
- import ConfigParser
- import sys
- config=ConfigParser.ConfigParser()
- config.add_section("book")
- config.set("book","title","這是標題")
- config.set("book","author","大頭爸爸")
- config.add_section("size")
- config.set("size","size",1024)
- config.write(sys.stdout)
執行結果
- [book]
- title = 這是標題
- author = 大頭爸爸
- [size]
- size = 1024
ConfigParser方法
- 1、config=ConfigParser.ConfigParser()
- 創建ConfigParser實例
- 2、config.sections()
- 返回配置文件中節序列
- 3、config.options(section)
- 返回某個項目中的所有鍵的序列
- 4、config.get(section,option)
- 返回section節中,option的鍵值
- 5、config.add_section(str)
- 添加一個配置文件節點(str)
- 6、config.set(section,option,val)
- 設置section節點中,鍵名為option的值(val)
- 7、config.read(filename)
- 讀取配置文件
- 8、config.write(obj_file)
- 寫入配置文件
綜合實例
- #coding=utf-8
- import ConfigParser
- def writeConfig(filename):
- config = ConfigParser.ConfigParser()
- # set db
- section_name = 'db'
- config.add_section( section_name )
- config.set( section_name, 'dbname', 'MySQL')
- config.set( section_name, 'host', '127.0.0.1')
- config.set( section_name, 'port', '80')
- config.set( section_name, 'password', '123456')
- config.set( section_name, 'databasename', 'test')
- # set app
- section_name = 'app'
- config.add_section( section_name )
- config.set( section_name, 'loggerapp', '192.168.20.2')
- config.set( section_name, 'reportapp', '192.168.20.3')
- # write to file
- config.write( open(filename, 'a') )
- def updateConfig(filename, section, **keyv):
- config = ConfigParser.ConfigParser()
- config.read(filename)
- print config.sections()
- for section in config.sections():
- print "[",section,"]"
- items = config.items(section)
- for item in items:
- print "\t",item[0]," = ",item[1]
- print config.has_option("dbname", "MySQL")
- print config.set("db", "dbname", "11")
- print "..............."
- for key in keyv:
- print "\t",key," = ", keyv[key]
- config.write( open(filename, 'r+') )
- if __name__ == '__main__':
- file_name = 'test.ini'
- writeConfig(file_name)
- updateConfig(file_name, 'app', reportapp = '192.168.100.100')
- print "end__"