熟悉使用ConfigParser庫讀寫配置文件
阿新 • • 發佈:2018-05-30
top lean () 使用 setting utf-8 sel [] int
Python的配置文件
配置文件
setting.ini文件是一個純文本
[DataBase1]
username = admin
passwors = root
[DataBase2]
hostname = 127.0.0.1
port = 3306
這是一般配置文件的常見的格式,[]裏面叫做Section部分的名稱,整個[]以及下面的每一項(每一個key=value,叫做Option)都是一個Section區域
Python讀取配置文件
#引入依賴庫,Python2.x引入ConfigParser Python3.x引入configparser
try:
import ConfigParser as cReader
except Exception as reason:
import configparser as cReader
要實現的功能
# -*- coding:utf-8 -*-
"""
自定義配置文件讀取庫
調用ConfigParser,只實現常用功能,作為學習ConfigParser和自用庫
"""
#引入依賴庫,Python2.x引入ConfigParser Python3.x引入configparser
import json
try:
import ConfigParser as cReader
except Exception as reason:
import configparser as cReader
#定義類和函數
class ConfigChannel:
"""建立配置讀取類"""
def __init__(self, configFile):
"""建立ConfigChannel對象"""
try:
self.configFile = configFile
self.channel = cReader.SafeConfigParser()
self.channel.read(open (configFile))
except Exception as reason:
raise
def getOption(self, Section, Option):
"""獲取某區域的某一項配置"""
try:
return self.channel.getfloot(Section, Option)
except Exception as reason:
try:
return self.channel.getint(Section, Option)
except Exception as reason:
try:
return self.channel.getboolean(Section, Option)
except Exception as reason:
return self.channel.get(Section, Option)
def hasOption(self, Section, Option):
"""判斷是否存在這個Option"""
try:
return self.channel.has_option(Section, Option)
except Exception as reason:
raise
def showOptions(self):
"""展示所有Options"""
try:
return self.channel.options()
except Exception as reason:
raise
def setOption(self, Section, Option, Value):
"""會寫或增加配置某區域某一項配置"""
try:
self.channel.set(Section, Option, Value)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise
def rmvOption(self, Section, Option):
"""刪除某一個配置項"""
try:
self.channel.remove_option(Section, Option)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise
def hasSection(self, Section):
"""判斷是否存在這個Section"""
try:
return self.channel.has_section(Section)
except Exception as reason:
raise
def showOptions(self):
"""展示所有Sections"""
try:
return self.channel.sections()
except Exception as reason:
raise
def addSection(self, Section):
"""增加一個配置區域"""
try:
self.channel.add_section(Section)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise
def rmvSection(self, Section):
"""刪除一個配置區域"""
try:
self.channel.remove_section(Section)
self.channel.write(open(self.configFile, "w"))
except Exception as reason:
raise
def getConfigFromString(self, string):
"""從字符串讀取配置並寫入"""
try:
_dictionary = json.loads(string)
return self.getConfigFromDictionary(_dictionary)
except Exception as reason:
pass
try:
configList = string.split(":")
if len(configList) != 3 or ‘‘ in configList:
raise Exception
Section = configList[0]
Option = configList[1]
Value = configList[2]
if self.hasSection(Section):
self.setOption(Section, Option, Value)
else:
self.addSection(Section)
self.setOption(Section, Option, Value)
except Exception as reason:
raise
def getConfigFromDictionary(self, dictionary):
"""從字典讀取配置並寫入"""
if not isinstance(dictionary, dict):
raise
try:
for key in dictionary:
if not isinstance(dictionary.get(key), dict):
continue
if not self.channel.hasSection(key):
self.addSection(key)
for subkey in dictionary[key]:
self.channel.setOption(key, subkey,
dictionary[key][subkey])
except Exception as reason:
raise
熟悉使用ConfigParser庫讀寫配置文件