首頁 後端開發 Python教學 python shelve模組

python shelve模組

Dec 07, 2016 am 11:16 AM
python

shelve 
shelve是一個簡單的資料儲存方案,他只有一個函數就是open(),這個函數接收一個參數就是檔名,然後回傳一個shelf對象,你可以用他來儲存東西,就可以簡單的把他當作一個字典,當你儲存完畢的時候,就呼叫close函數來關閉 
這個有一個潛在的小問題,如下: 
[python] view plaincopy 
>>> import shelve  
>>> s = shelve. open('test.dat')  
>>> s['x'] = ['a', 'b', 'c']  
>>> s['x'].append('d')  
>>> s['x']  
['a', 'b', 'c']  
儲存的d到哪裡去了呢?其實很簡單,d沒有寫回,你把['a', 'b', 'c']存到了x,當你再次讀取s['x']的時候,s['x']只是一個拷貝,而你沒有將拷貝寫回,所以當你再次讀取s['x']的時候,它又從源中讀取了一個拷貝,所以,你新修改的內容並不會出現在拷貝中,解決的辦法就是,第一個是利用一個緩存的變量,如下所示 
[python] view plaincopy 
>>> temp = s['x']  
>>> temp.append('d')  
>>> s['x'] = temp  
>>> s['x']  
['a', 'b', 'c', 'd']  
在python2.4中有了另一個的方法,就是把open方法的writeback參數的值賦為True,這樣的話,你open後所有的內容都將在cache中,當你close的時候,將全部一次性寫到硬碟裡面。如果資料量不是很大的時候,建議這麼做。

下面是一個基於shelve的簡單資料庫的程式碼 
[python] view plaincopy 
#database.py  
import sys, shelve  
 user for data and store it in the shelf object 
    """  
    pid = raw_input('Enter unique ID number: ')  
   )   person['age'] = raw_input( 'Enter age: ')  
    person['phone'] = raw_input('Enter phone number: ')  
    db[pid] = person  
   Query user for ID and desired field , and fetch the corresponding data from 
    the shelf object 
    """  
    pid = raw_input('Enter number: ')  ')  phone) ')  
field = field.strip().lower()  
    print field.capitalize() + ':',   
        db[pid able commons are: '  
    print ' store  :Stores information about a person'  
    print 'lookup :Looks up a person from ID number'  
    print 'quit   print:Save s and exave '  
  
def enter_command():  
    cmd = raw_input ('Enter command (? for help): ')  
    cmd = cmd.strip().lower()  
    return cmd  
  🠎, main(():openomain(com):open 
    try:
        while True:  
            cmd = enter_command()             store_person(database)  
elif cmd == '查找':  
                Lookup_person(資料庫)  
            print_help()  
            elif cmd == '退出':      
       database.close()  
if __name__ = = '__main__': main()

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

模板化的優點和缺點有哪些? 模板化的優點和缺點有哪些? May 08, 2024 pm 03:51 PM

模板化的優點和缺點有哪些?

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2 Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2 Jul 01, 2024 am 07:22 AM

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2

怎麼下載deepseek 小米 怎麼下載deepseek 小米 Feb 19, 2025 pm 05:27 PM

怎麼下載deepseek 小米

deepseek怎麼問他 deepseek怎麼問他 Feb 19, 2025 pm 04:42 PM

deepseek怎麼問他

evaluate函數怎麼保存 evaluate函數怎麼保存 May 07, 2024 am 01:09 AM

evaluate函數怎麼保存

NET40是什麼軟體 NET40是什麼軟體 May 10, 2024 am 01:12 AM

NET40是什麼軟體

deepseek該怎麼搜索 deepseek該怎麼搜索 Feb 19, 2025 pm 05:18 PM

deepseek該怎麼搜索

瀏覽器插件是什麼語言寫的 瀏覽器插件是什麼語言寫的 May 08, 2024 pm 09:36 PM

瀏覽器插件是什麼語言寫的

See all articles