溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python第九天學習總結(jié)

發(fā)布時間:2020-07-07 22:09:39 來源:網(wǎng)絡(luò) 閱讀:359 作者:zhaoxiaobu 欄目:編程語言

1.hashlib模塊
#hashlib模塊:摘要算法,它通過一個函數(shù),把任意長度的數(shù)據(jù)轉(zhuǎn)換為一個長度固定的數(shù)據(jù)串(通常用16進制的字符串表示)
##hashlib模塊的應(yīng)用
###加密
import hashlib
md5_obj = hashlib.md5() # 選擇了md5算法,sha算法的使用類似
s = input('>>>')
md5_obj.update(s.encode('utf-8')) #以s做鹽進行加密,提高安全性
print(md5_obj.hexdigest()) #打印摘要

hashlib加密的特點:
1.使用相同的算法對同一個字符串進行摘要在任意時刻 任意平臺 任意語言結(jié)果總是不變的
2.這個摘要過程不可逆
3.對于不同的數(shù)據(jù)的計算結(jié)果總是不同的

###校驗文件一致性
import os
import hashlib
def get_md5(file,n = 10240):
with open(file, 'rb') as f1:
md5_obj = hashlib.md5()
file_size = os.path.getsize(file)
while file_size>0:
md5_obj.update(f1.read(n))
file_size -= n
return md5_obj.hexdigest()

def compare(file1,file2):
return get_md5(file1) == get_md5(file2)

2.configparser模塊
#configparser模塊:設(shè)置配置文件
import configparser

config = configparser.ConfigParser()

config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9',
'ForwardX11':'yes'
}

config['bitbucket.org'] = {'User':'hg'}

config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}

with open('example.ini', 'w') as configfile:

config.write(configfile)

產(chǎn)生了一個example.ini文件,內(nèi)容如下:
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no
#可通過代碼對文件進行增刪查改操作,和操作字典方法類似

3.logging模塊
#logging模塊:日志格式的模塊
##簡單配置方式
import logging
logging.debug('debug message') # 調(diào)試模式
logging.info('info message') # 基礎(chǔ)正常的信息
logging.warning('warning message') # 警告信息
logging.error('error message') # 錯誤信息
logging.critical('critical message') # 批判的 嚴重錯誤

logging.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='test.log',
filemode='a')

##logger對象的配置方式
*# 創(chuàng)建一個logger對象
創(chuàng)建一個屏幕管理對象
創(chuàng)建一個文件管理對象
創(chuàng)建一個格式對象

屏幕管理對象 + 一個格式對象
文件管理對象 + 一個格式對象

logger對象*
屏幕管理對象
文件管理對象

logger = logging.getLogger() # 創(chuàng)建一個logger對象
sh = logging.StreamHandler() # 創(chuàng)建一個屏幕管理對象
fh = logging.FileHandler('test2.log',encoding='utf-8') # 創(chuàng)建一個文件管理對象
fomatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # 創(chuàng)建一個格式對象

sh.setFormatter(fomatter) # 屏幕管理對象 + 一個格式對象
fh.setFormatter(fomatter) # 文件管理對象 + 一個格式對象
sh.setLevel(logging.WARNING) #屏幕管理對象的輸出級別
fh.setLevel(logging.INFO) # 文件管理對象的輸出級別

logger.addHandler(sh) #logger對象+屏幕管理對象
logger.addHandler(fh) #logger對象+文件管理對象
logger.setLevel(logging.DEBUG)

logger.debug('你好') # 調(diào)試模式
logger.info('info message') # 基礎(chǔ)正常的信息
logger.warning('warning message') # 警告信息
logger.error('error message') # 錯誤信息
logger.critical('critical message') # 批判的 嚴重錯誤

4.序列化模塊
#將數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)換成字符串是序列化,將字符串轉(zhuǎn)換成數(shù)據(jù)結(jié)構(gòu)是反序列化。序列化主要是為了文件存儲和網(wǎng)絡(luò)傳輸。相關(guān)的模塊有json pickle shelve
import json
lst = [1,2,3,'bbb']
dic = {'a':1,2:3}
ret1= json.dumps(lst) #dumps將數(shù)據(jù)類型轉(zhuǎn)字符串
print(ret1,type(ret1))
ret2= json.dumps(dic)
print(ret2,type(ret2))

res1 = json.loads(ret1) #loads反序列化過程
res2= json.loads(ret2)
print(res1,type(res1))
print(ret2,type(res2))
##json 只支持有限的數(shù)據(jù)類型 字典 列表 數(shù)字類型
f = open('json_file','w')
json.dump([1,2,3],f) #dump操作和文件相關(guān)
f.close()

f = open('json_file','r')
content = json.load(f) #load操作和文件相關(guān)
f.close()

#pickle和json用法類似,pickle只支持python,幾乎支持所有數(shù)據(jù)類型。json所有的語言都通用,支持的數(shù)據(jù)類型有限

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI