課堂筆記隨記10-pycharm使用技巧v0_第1頁
課堂筆記隨記10-pycharm使用技巧v0_第2頁
課堂筆記隨記10-pycharm使用技巧v0_第3頁
課堂筆記隨記10-pycharm使用技巧v0_第4頁
課堂筆記隨記10-pycharm使用技巧v0_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、思路分解:1- 獲取文件中每一行的信息:學生 id 課程 id 簽到時間2- 按照要求去統(tǒng)計3- 輸出step over-單步運行每一行為一步def func():pr(1)pr(2)pr(3)pr(4)#1- 獲取文件中每一行的信息:學生 id 課程 id 簽到時間fileDir = rG:0005_1.txtdef putInfoToDict(fileName):resDict = #1- 打開文件with open(fileName) as fo:#2-所有行l(wèi)ines = fo.read().splitlines()#返回是 list#3- 對每一行進去提取數(shù)據(jù)-遍歷for line

2、in lines:# info = #臨時的字典line =line.replace(t,).replace(,).replace(),).replace(,)#切割 ,temp = line.split(,)checkTime = temp0lessonId =(temp1)userId =(temp2)#2- 按照要求去統(tǒng)計info = lessonid:lessonId, checkime:checkTime# infolessonid = lessonId# infocheckime = checkTimeif userId in resDict:#如果學生 id 在里面resDict

3、userId.append(info)else:#新增鍵值對import loggingLOG_FORMAT = %(asctime)s - %(levelname)s - %(message)sDATE_FORMAT = %m/%d/%Y %H:%M:%S %plog_file = G:/basic20200315.loglogging.basicConfig(filename=log_file,level=logging.DEBUG,formOG_FORMAT,datefmt=DATE_FORMAT)# logging.basicConfig(filename=log_file, leve

4、l=logging.DEBUG)logging.debug(this is a debugmsg!)(this is a infomsg!)logging.warning(this is a warn msg!)logging.error(this is a error msg!)logging.critical(this is a criticag!)時間級別重要數(shù)據(jù)異常信息等resDictuserId = info# func()return resDictres = putInfoToDict(fileDir)# pr(res)#完美打印import pprppr

5、.ppr(res)#json# import json# pr(json.dumps(res,indent=1)import loggingfrom logging.handlers import RoingFileHandlerimport datetime# pr(datetime.datetime.now().strftime(%Y_%m_%d_%H_%M_%S)STYLE = fore:# 前景色black: 30,# 黑色red: 31,# 紅色green: 32,# 綠色yellow: 33,# 黃色blue: 34,# 藍色purple: 35,# 紫紅色cyan: 36,# 青

6、藍色white: 37,# 白色,back :# 背景black: 40, # 黑色red: 41, # 紅色green: 42, # 綠色yellow: 43, # 黃色blue: 44, # 藍色purple: 45, # 紫紅色cyan: 46, # 青藍色white: 47, # 白色,mode :# 顯示模式mormal: 0,# 終端默認設(shè)置 bold: 1,#顯示 underline : 4,# 使用下劃線blink: 5,# 閃爍invert: 7,# 反白顯示hide: 8,# 不可見,default :end : 0,def UseStyle(string, mode =

7、, fore = , back = ):mode = %s % STYLEmodemode if STYLEmode.get(mode) elsefore = %s % STYLEforefore if STYLEfore.get(fore) elseback = %s % STYLEbackback if STYLEback.get(back) elsestyle = ;.join(s for s in mode, fore, back if s)style = 033%sm % style if style else end= 033%sm % STYLEdefaultend if sty

8、le else return %s%s%s % (style, string, end)debug:最細微的信息到 debug 中,這個級別就是用來 debug 的,看程序在哪一次迭發(fā)生了錯誤,比如每次循環(huán)都輸出一些東西用 debug 級別info:級別用于 routines,也就是輸出 start finish 狀態(tài)改變等信息warn:輸出一些相對重要,但是不是程序 bug 的信息,比如輸入了錯誤的,或者連接較慢error:輸出程序 bug,打印異常信息critical:用于處理一些非常糟糕的事情,比如內(nèi)存溢出、磁盤已滿,這個一般較少使用class Logger:def init (self

9、,inLevel=DEBUG,inName= name ):self.logger = logging.getLogger(inName)self.logger.setLevel(inLevel)self.formatter = logging.Formatter(%(asctime)s - %(name)s- %(levelname)s - %(message)s)def addHand(self,source):self.logger.addHandler(source)def debug(self, msg):self.logger.debug(msg)def info(self, ms

10、g):(msg)def warning(self, msg):self.logger.warning(msg)def error(self, msg):self.logger.error(msg)#def critical(self, msg):self.logger.criticag)class FileLogger(Logger):#log 文件輸出類def init (self, logName):Logger. init (self, inLevel=DEBUG, inName= name )self.loggerName = logNameself.r

11、Handler =RoingFileHandler(_.log.format(self.loggerName,datetime.datetime.now().strftime(%Y_%m_%d_%H_%M_%S),maxBytes = 1*1024,backupCount =3,encoding=utf-8)self.rHandler.setFormatter(self.formatter)self.addHand(self.rHandler)class ConsoleLogger(Logger):#控制臺輸出日志類def init (self):Logger. init (self, inL

12、evel=DEBUG, inName= name )self.console = logging.StreamHandler()self.console.setFormatter(self.formatter)self.addHand(self.console)def debug(self, msg):self.logger.debug(UseStyle(msg, fore = black)#黑色def info(self, msg):(UseStyle(msg, fore = blue)#藍色def warning(self, msg):self.logger.warning(UseStyle(msg, fore = yellow)#黃色def error(self, msg):self.logger.error(UseStyle(msg, fore = red)#紅色def cr

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論