- [mysql]
- ;數(shù)據(jù)庫地址
- host=127.0.0.1
- ;數(shù)據(jù)庫鏈接端口
- port=3306
- ;數(shù)據(jù)庫用戶名
- user=root
- ;數(shù)據(jù)庫密碼
- password=123456
- ;默認鏈接的數(shù)據(jù)庫名
- db=autotest
Python ConfigParser類
在python里由標準模塊ConfigParser模塊提供的ConfigParser類實現(xiàn)對INI格式的文件進行讀寫,下面我們看看其主要的函數(shù),也是大家必須熟悉的。- # 初始化
- cf = ConfigParser.ConfigParser() # 讀取ini文件,path為要讀取的ini文件的路徑
- cf.read(path)
- # 獲取所有sections。即將配置文件中所有“[ ]”讀取到列表中
- s = cf.sections()
- # 獲取指定section的options。
- # 即將配置文件某個section內(nèi)key 讀取到列表中
- o = cf.options("mysql")# 獲取指定section 的配置信息v = cf.items("msyql")# 按照類型讀取指定section 的option 信息# 同樣的還有g(shù)etfloat、getbooleandb_host = cf.get("mysql", "host")
- db_port = cf.getint("mysqldb", "port")
- db_user = cf.get("mysql", "user")
- db_pass = cf.get("mysql", "password")
- # 設(shè)置某個option 的值。(記得最后要保存)
- cf.set("mysql", "password", "654321")
- # 添加一個section。(同樣要保存)
- cf.add_section('oracle')
- cf.set('oracle', 'host', '127.0.0.1')
- cf.set('oracle', 'port', '5555')
- # 移除section 或者option (同樣要保存)
- cf.remove_option('oracle','port')
- cf.remove_section('oracle')
實例
下面我們對ConfigParser進行下簡單的封裝,形成我們自己的ini解析類,并演示如何使用。- #-*- coding:utf-8 -*-
- from configparser import ConfigParser
- import os
- class LYMINIParser:
- def __init__(self, path):
- self.path = path
- self.ini = ConfigParser()
- self.ini.read(self.path)
- # 獲取sections列表
- def get_sections(self):
- if self.ini:
- return self.ini.sections()
- # 獲取指定的section的options列表
- def get_options_by_section(self, section):
- if self.ini:
- return self.ini.options(section)
- # 獲取指定section的配置信息列表
- def get_section_items(self, section):
- if self.ini:
- return self.ini.items(section)
- # 按類型讀取配置信息
- # 返回字符串類型
- def get_string(self, section, option):
- if self.ini:
- return self.ini.get(section, option)
- # 返回int類型
- def get_int(self, section, option):
- if self.ini:
- return self.ini.getint(section, option)
- # 返回float類型
- def get_float(self, section, option):
- if self.ini:
- return self.ini.getfloat(section, option)
- # 返回bool類型
- def get_boolean(self, section, option):
- if self.ini:
- return self.ini.getboolean(section, option)
- # 新增section
- def add_section(self, section):
- if self.ini:
- self.ini.add_section(section)
- self.ini.write(open(self.path, "w"))
- # 設(shè)置指定option值
- def set_option(self, section, option, value):
- if self.ini:
- self.ini.set(section, option, value)
- self.ini.write(open(self.path, "w"))
- # 刪除指定section
- def remove_section(self, section):
- if self.ini:
- self.ini.remove_section(section)
- self.ini.write(open(self.path, "w"))
- # 刪除指定option
- def remove_option(self, section, option):
- if self.ini:
- self.ini.remove_option(section, option)
- self.ini.write(open(self.path, "w"))
- if __name__ == "__main__":
- print("python ini標準庫解析實例")
- # 如果存在mysql.ini先刪除,方便下列代碼的運行
- if os.path.exists("mysql.ini"):
- os.remove("mysql.ini")
- # 我們先寫一些數(shù)據(jù)到mysql.ini中
- ini = LYMINIParser("mysql.ini")
- # 先加一個mysql section
- mysql_section = "mysql"
- ini.add_section(mysql_section)
- # 在mysql section下寫入一些配置信息
- ini.set_option(mysql_section, "host", "127.0.0.1")
- ini.set_option(mysql_section, "port", "3306")
- ini.set_option(mysql_section, "db", "autotesting")
- ini.set_option(mysql_section, "user", "root")
- ini.set_option(mysql_section, "password", "123456")
- # 再添加一個oracle section
- oracle_section = "oracle"
- ini.add_section(oracle_section)
- # oracle section下寫入一些配置信息
- ini.set_option(oracle_section, "host", "127.0.0.1")
- ini.set_option(oracle_section, "port", "1520")
- ini.set_option(oracle_section, "db", "auto_ui")
- ini.set_option(oracle_section, "user", "sa")
- ini.set_option(oracle_section, "password", "123456")
- # 獲取下所有的section,并在console輸出
- sections = ini.get_sections()
- print(sections)
- # 遍歷各個section下的options,并在console中輸出
- print("---" * 20) for sec in sections:
- print(sec, " 中的options為: ")
- options = ini.get_options_by_section(sec)
- print(options)
- print("---" * 20)
- # 獲取各個section下的配置信息
- for sec in sections:
- print(sec, " 中的配置信息為: ")
- items = ini.get_section_items(sec)
- print(items)
- print("---" * 20)
- # 獲取指定的option值這里演示讀取host和port
- host = ini.get_string("mysql", "host")
- port = ini.get_int("mysql", "port")
- print("類型: ", type(host), " ", type(port))
- print(host, " ", port)
- # 刪除mysql中的host配置
- ini.remove_option("mysql", "host")
- # 刪除oracle section
- ini.remove_section("oracle")
- # 修改mysql port的值為4000
- ini.set_option("mysql", "port", "4000")
- # 最終mysql.ini中的文件內(nèi)容如下
- #[mysql]
- #port = 4000
- #db = autotesting
- #user = root
- #password = 123456
小結(jié)本文對ini格式的解析進行了較為完整的演示,大家可以根據(jù)需要對示例中的進一步封裝優(yōu)化掌握。