python第九天學習總結

來源:互聯網
上載者:User

標籤:igp   formatter   內容   總結   選擇   字元   加密   支援   inpu   

1.hashlib模組
#hashlib模組:摘要演算法,它通過一個函數,把任意長度的資料轉換為一個長度固定的資料串(通常用16進位的字串表示)
##hashlib模組的應用
###加密
import hashlib
md5_obj = hashlib.md5() # 選擇了md5演算法,sha演算法的使用類似
s = input(‘>>>‘)
md5_obj.update(s.encode(‘utf-8‘)) #以s做鹽進行加密,提高安全性
print(md5_obj.hexdigest()) #列印摘要

hashlib加密的特點:
1.使用相同的演算法對同一個字串進行摘要在任意時刻 任意平台 任意語言結果總是不變的
2.這個摘要過程無法復原
3.對於不同的資料的計算結果總是不同的

###校正檔案一致性
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模組:設定設定檔
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)

產生了一個example.ini檔案,內容如下:
[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‘) # 偵錯模式
logging.info(‘info message‘) # 基礎正常的資訊
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對象的配置方式
*# 建立一個logger對象
建立一個螢幕管理對象
建立一個檔案管理對象
建立一個格式對象

螢幕管理對象 + 一個格式對象
檔案管理對象 + 一個格式對象

logger對象*
螢幕管理對象
檔案管理對象

logger = logging.getLogger() # 建立一個logger對象
sh = logging.StreamHandler() # 建立一個螢幕管理對象
fh = logging.FileHandler(‘test2.log‘,encoding=‘utf-8‘) # 建立一個檔案管理對象
fomatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘) # 建立一個格式對象

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(‘你好‘) # 偵錯模式
logger.info(‘info message‘) # 基礎正常的資訊
logger.warning(‘warning message‘) # 警告資訊
logger.error(‘error message‘) # 錯誤資訊
logger.critical(‘critical message‘) # 批判的 嚴重錯誤

4.序列化模組
#將資料結構轉換成字串是序列化,將字串轉換成資料結構是還原序列化。序列化主要是為了檔案儲存體和網路傳輸。相關的模組有json pickle shelve
import json
lst = [1,2,3,‘bbb‘]
dic = {‘a‘:1,2:3}
ret1= json.dumps(lst) #dumps將資料類型轉字串
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 只支援有限的資料類型 字典 列表 數字類型
f = open(‘json_file‘,‘w‘)
json.dump([1,2,3],f) #dump操作和檔案相關
f.close()

f = open(‘json_file‘,‘r‘)
content = json.load(f) #load操作和檔案相關
f.close()

#pickle和json用法類似,pickle只支援python,幾乎支援所有資料類型。json所有的語言都通用,支援的資料類型有限

python第九天學習總結

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.