configparser模組
import configparserret = configparser.configparser()
ret["a"] =
ret["b"] =
ret["c"] =
with open ("file","w") as f:
ret.write(f)
自動生成檔案
[a]1 = 2
3 = 4
[b]5 = 6
[c]7 = 8
按照字典的方式進行增刪改操作
import configparserret = configparser.configparser()
ret["a"] =
ret["b"] =
ret["c"] =
with open ("file","w") as f:
ret.write(f)
ret.read("c")
ret.add_section("d")
ret.remove_section("a")
ret.write(open("file","w")) #增刪改之後必須進行這一步操作
檔案[b]
5 = 6
[c]7 = 8
[d]
logging模組
import logginglogging.basicconfig(level=logging.debug,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %y %h:%m:%s',
filename='/tmp/test.log',
filemode='w')
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
配置引數
logging.basicconfig()函式中可通過具體引數來更改logging模組預設行為,可用引數有:filename:用指定的檔名建立filedhandler,這樣日誌會被儲存在指定的檔案中。
filemode:檔案開啟方式,在指定了filename時使用這個引數,預設值為「a」還可指定為「w」。
format:指定handler使用的日誌顯示格式。
datefmt:指定日期時間格式。
level:設定rootlogger(後邊會講解具體概念)的日誌級別
stream:用指定的stream建立streamhandler。可以指定輸出到sys.stderr,sys.stdout或者檔案(f=open(『test.log』,』w』)),預設為sys.stderr。若同時列出了filename和stream兩個引數,則stream引數會被忽略。
format引數中可能用到的格式化串:
%(name)s logger的名字
%(levelno)s 數字形式的日誌級別
%(levelname)s 文字形式的日誌級別
%(pathname)s 呼叫日誌輸出函式的模組的完整路徑名,可能沒有
%(filename)s 呼叫日誌輸出函式的模組的檔名
%(module)s 呼叫日誌輸出函式的模組名
%(funcname)s 呼叫日誌輸出函式的函式名
%(lineno)d 呼叫日誌輸出函式的語句所在的**行
%(created)f 當前時間,用unix標準的表示時間的浮 點數表示
%(relativecreated)d 輸出日誌資訊時的,自logger建立以 來的毫秒數
%(asctime)s 字串形式的當前時間。預設格式是 「2003-07-08 16:49:45,896」。逗號後面的是毫秒
%(thread)d 執行緒id。可能沒有
%(threadname)s 執行緒名。可能沒有
%(process)d 程序id。可能沒有
%(message)s使用者輸出的訊息
logger物件配置
import logginglogger = logging.getlogger()
# 建立乙個handler,用於寫入日誌檔案
fh = logging.filehandler('test.log',encoding='utf-8')
# 再建立乙個handler,用於輸出到控制台
ch = logging.streamhandler()
formatter = logging.formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setlevel(logging.debug)
fh.setformatter(formatter)
ch.setformatter(formatter)
logger.addhandler(fh) #logger物件可以新增多個fh和ch物件
logger.addhandler(ch)
logger.debug('logger debug message')
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')
ConfigParser模組教程
configparser 模組用於操作配置檔案 注 parser漢譯為 解析 之意。配置檔案的格式與windows ini檔案類似,可以包含乙個或多個節 section 每個節可以有多個引數 鍵 值 book title configparser模組教程 time 2012 09 20 22 04 ...
ConfigParser模組教程
目錄 configparser 模組用於操作配置檔案 注 parser漢譯為 解析 之意。配置檔案的格式與windows ini檔案類似,可以包含乙個或多個節 section 每個節可以有多個引數 鍵 值 plain view plain copy book title configparser模組...
configparser模組總結
configparser模組使用來讀取寫入配置檔案的,其配置檔案的結構為 section1 key1 value1 key2 value2 section2 key1 value1 key2 value2其中 包圍的部分為section,是區分各個配置的標誌,下面的值是以key value的形式存在...