Python各种包学习
Logging
logging的level
DEBUG>INFO>WARNING>ERROR>CRITICAL
import logginglogging.basicConfig(level=logging.ERROR)logging.debug("this is debug") logging.warning("hello") logging.info("hi")無(wú)輸出因?yàn)镋RROR<輸出任意一個(gè)等級(jí)logging.basicConfig
filename代表輸出的都放到這個(gè)文件
filemode='w'代表寫入模式,每次寫入前都要先把文件清空寫入,而不設(shè)置則是'a'append追加模式
import loggingprint("this is logging")logging.basicConfig(filename='log.log',filemode='w',level=logging.DEBUG) logging.debug("this is the debug log")logging.debug變量
需要配置encoding='utf-8'否則亂碼
import loggingprint("this is logging")logging.basicConfig(filename='log.log',encoding='utf-8',filemode='a',level=logging.DEBUG) logging.debug("this is the debug log") name='qlxr' age=24 logging.debug("姓名 %s 年齡 %d",name,age) logging.debug("姓名 %s 年齡 %d"%(name,age)) logging.debug("姓名 {} 年齡 {}".format(name,age)) logging.debug(f"姓名 {name} 年齡 {age}")DEBUG:root:this is the debug log DEBUG:root:���� qlxr ���� 24 DEBUG:root:this is the debug log DEBUG:root:���� qlxr ���� 24 DEBUG:root:���� qlxr ���� 24 DEBUG:root:���� qlxr ���� 24 DEBUG:root:���� qlxr ���� 24 DEBUG:root:this is the debug log DEBUG:root:姓名 qlxr 年齡 24 DEBUG:root:姓名 qlxr 年齡 24 DEBUG:root:姓名 qlxr 年齡 24 DEBUG:root:姓名 qlxr 年齡 24logging%()s特定字符
%(asctime)s:時(shí)間 datefmt(設(shè)置時(shí)間格式)
%(levelname)s:等級(jí)
%(filename)s:運(yùn)行的文件
%(lineno)s:程序運(yùn)行行數(shù)
%(message)s:debug信息
import logging logging.basicConfig(format="%(asctime)s %(levelname)s %(filename)s %(lineno)s %(message)s",datefmt="%Y-%m-%d %H:%M:%S",level=logging.DEBUG) logging.debug("清涼夏日")2022-03-21 19:56:54 DEBUG rizhistudy.py 23 清涼夏日深入
# -*- codeing = utf-8 -*- # @Time : 2022/3/22 20:48 # @Author : 何瀟然 # @File : demo2duixiang.py # @Software: PyCharm import logging#記錄器 logger=logging.getLogger("applog") logger.setLevel(logging.DEBUG)#logger先行過(guò)濾#處理器 consolehandler=logging.StreamHandler() consolehandler.setLevel(logging.DEBUG)#控制臺(tái)輸出DEBUG級(jí)別#如果沒(méi)有給handler指定日志級(jí)別,將使用logger級(jí)別 filehandler=logging.FileHandler(filename="log.log") filehandler.setLevel(logging.INFO)#文件中輸出INFO級(jí)別#formatter格式 formatter=logging.Formatter("%(asctime)s %(levelname)s %(filename)s %(lineno)s %(message)s")#給處理器設(shè)置格式 consolehandler.setFormatter(formatter) filehandler.setFormatter(formatter)#記錄器設(shè)置處理器 logger.addHandler(consolehandler) logger.addHandler(filehandler)logger.debug("this is debug log") logger.info("this is info log") logger.warning("this is warning log") logger.error("this is error log") logger.critical("this is critical log")通過(guò)配置文件進(jìn)行l(wèi)ogging
attention!:配置文件里禁制使用中文 注釋也不行 否則讀取格式會(huì)出錯(cuò)
[loggers] keys=root,applog[handlers] keys=fileHandler,consoleHandler,TimedRotatingFileHandler[formatters] keys=simpleFormatter[logger_root] level=DEBUG handlers=consoleHandler,fileHandler#[logger_applog] level=DEBUG handlers=TimedRotatingFileHandler,consoleHandler qualname=applog#logging.getLogger('applog') propagate=0[handler_consoleHandler] class=StreamHandler args=(sys.stdout,) level=DEBUG formatter=simpleFormatter[handler_TimedRotatingFileHandler] class=handlers.TimedRotatingFileHandler args=('applog.log','midnight',1,0)#midnight每到12點(diǎn)午夜 delay 1s推遲1s 0保存為log的日志并不刪除 level=DEBUG formatter=simpleFormatter[handler_fileHandler] class=FileHandler args=('rootlog.log','w') formatter=simpleFormatter[formatter_simpleFormatter] formatter=%(asctime)s|%(levelname)8s|%(filename)s[:%(lineno)d]|%(message)s datefmt=%Y-%m-%d %H:%M:%S?Numpy
NumPy最重要的一個(gè)特點(diǎn)就是其N維數(shù)組對(duì)象(即ndarray),該對(duì)象是一個(gè)快速而靈活的大數(shù)據(jù)集容器。你可以利用這種數(shù)組對(duì)整塊數(shù)據(jù)執(zhí)行一些數(shù)學(xué)運(yùn)算,其語(yǔ)法跟標(biāo)量元素之間的運(yùn)算一樣。
data3=np.random.randn(2,3,4) print(data3.shape) print(data3)[[[-0.22280369 -0.64911247 -1.82073536 -0.11877401][ 0.05232314 -1.06563146 1.45031713 0.42216378][-0.0231252 -1.22874131 0.9428394 1.00117319]][[-0.27866493 -1.26631694 -2.19427966 -0.41571251][ 0.46575417 -0.28105551 1.25212153 -1.72946444][ 0.53935346 -0.21542028 1.13053317 0.68798606]]]使用random.randn(x,y)x和y代表維度(幾行幾列),函數(shù)會(huì)返回一個(gè)[0.0,1.0)之間的一個(gè)隨機(jī)數(shù)值。(x,y,z)x代表幾組,y、z代表維度
data1.shape 查看data1的維度
data1.type 查看data1數(shù)據(jù)類型
np.random
#需求:人的步伐為1或者-1 輸出1000步的漫步圖 import random import matplotlib.pyplot as pltposition=0 walk=[position] steps=1000 for i in range(steps):step=1 if random.randint(0,1) else -1position+=stepwalk.append(position) walk=np.array(walk) print(walk) print("min"+str(walk.min())) print("max"+str(walk.max())) plt.plot(walk[:200]) plt.show()random.randint(x,y)生成[x,y]范圍內(nèi)的整數(shù)
np.random.randint(x,y)生成[x,y)范圍內(nèi)的整數(shù)
import random import matplotlib.pyplot as pltsteps=100 nsteps=10 data=np.random.randint(0,2,size=(nsteps,steps)) dataresult=np.where(data>0,1,-1) walks=dataresult.cumsum(1) print(data) print(walks) print(walks.max()) print(walks.min())- cumsum(0):實(shí)現(xiàn)0軸上的累加:以最外面的數(shù)組元素為單位,以[[1,2,3],[8,9,12]]為開始實(shí)現(xiàn)后面元素的對(duì)應(yīng)累加
- cumsum(1):實(shí)現(xiàn)1軸上的累加:以中間數(shù)組元素為單位,以[1,2,3]為開始,實(shí)現(xiàn)后面元素的對(duì)應(yīng)累加
- cumsum(2):實(shí)現(xiàn)2軸上的累加:以最里面的元素為累加單位,即1為開始,實(shí)現(xiàn)后面的元素累加
總結(jié)
以上是生活随笔為你收集整理的Python各种包学习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 牛客网习题集 - Wannafly挑战赛
- 下一篇: python 基础之文件读操作