本篇文章帶給大家的內容是關於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中文網其他相關文章!

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版
好用的JavaScript開發工具