Python中shelve模組的簡單介紹(附範例)
本篇文章帶給大家的內容是關於Python中shelve模組的簡單介紹(附範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
shelve:物件持久化的保存的模組,將物件儲存到檔案裡 (預設的資料儲存檔案為二進位),可持久化任何pickle可支援的Python資料格式
shelve 中唯一的方法:
shelve.open(filename,flag = 'c', protocol = None , writebake = False)
filename | #關聯的檔案路徑 |
flag | 'r' :以唯讀模式開啟一個已經存在的資料儲存檔案 |
' w' :以讀寫模式開啟一個已經存在的資料儲存檔案 | |
# 'c' :(預設)以讀寫模式開啟資料儲存文件,如果不存在則建立 | |
'n' :總是以讀寫模式開啟並且建立新的空白資料儲存檔案 | |
protocol | 表示序列化資料所使用的協議,預設為None(pickle v3) |
writebake | 表示是否開啟回寫功能 |
##1.檔案可以像字典一樣儲存key - value (註:key 必須為字串,value 可以是任何資料型別)
import shelve date = shelve.open('family.txt') # Python的自处理系统会自动生成三个文件 date['father'] = 'Presly' # 默认为创建并且写入“c” date['mather'] = 'Vera' date['baby'] = [123, ] date.close() m = shelve.open('family.txt', falg= 'r', writebake=True) # 当writebake设置为True时,文件里才能直接添加 print(m['baby']) m['baby'].append(345) print(m['father']) print('-------------') for key, value in m.items(): # 以字典的格式 print(key, ':', value) m.close()
[123] Presly ------------- father : Presly mather : Vera baby : [123,345]
2. #shelve 的序列化
可以把類別的資料序列化,然後再反序列化出元素
與pickle不同的是,pickle只能按照dump順序,load出元素,而shelve可以直接重複拿出不同或相同存進檔案的key值,
3. shelve 可以進行類似函式庫,增,刪,改,查
import shelve def store_information(database): info = {} ID = input('Enter the ID number:') info['name'] = input('Enter the name:') # 将name ,age , phone 存入字典info里 info['age'] = input('Enter the age:') info['phone'] = input('Enter the phone:') database[ID] = info # 用ID : info 存入 database文件 def lookup_information(database): ID = input('Enter the ID:') field = input('What would you like to know?(name,age,phone)') field = field.strip().lower() print(database[ID][field]) # 通过输入的ID与 field 直接打印结果 def print_help(): print('Please enter the help command:') print('store :store informatinon to database') print('lookup :look up information by numID') print('quit :save information and quit') print('? :print help command') def enter_command(): cmd = input('Enter command (? for help)') cmd = cmd.strip().lower() return cmd def main(): database = shelve.open('db.dat') try: while True: cmd = enter_command() if cmd == 'store': store_information(database) # 当if函数结束,自动跳转到cmd = enter_command()行 elif cmd == 'lookup': lookup_information(database) elif cmd == '?': print_help() elif cmd == 'quit': return # 跳出while循环 finally: database.close() if __name__ == '__main__': main()
以上是Python中shelve模組的簡單介紹(附範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

本文討論了諸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和請求等流行的Python庫,並詳細介紹了它們在科學計算,數據分析,可視化,機器學習,網絡開發和H中的用途

在Python中,如何通過字符串動態創建對象並調用其方法?這是一個常見的編程需求,尤其在需要根據配置或運行...
