Python使用shelve模塊實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)存儲(chǔ)的方法
本文實(shí)例講述了Python使用shelve模塊實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)存儲(chǔ)的方法。分享給大家供大家參考。具體分析如下:
Python的shelve模塊提供了一種簡(jiǎn)單的數(shù)據(jù)存儲(chǔ)方案,以dict(字典)的形式來(lái)操作數(shù)據(jù)。
#!/usr/bin/python import sys, shelve def store_person(db): """ Query user for data and store it in the shelf object """ pid = raw_input('Enter unique ID number:') person = {} person['name'] = raw_input('Enter name:') person['age'] = raw_input('Enter age:') person['phone'] = raw_input('Enter phone number:') db[pid] = person def lookup_person(db): """ Query user for ID and desired field, and fetch the corresponding data from the shelf object """ pid = raw_input('Enter unique ID number:') temp = db[pid] field = raw_input('Please enter name, age or phone:') field.strip().lower() print field.capitalize() + ': ', temp[field] def print_help(): print 'The avaliable commands are:' print 'store :Stores infomation about a person' print 'lookup :Looks up a person form ID number' print 'quit :Save changes and exit' print '? :Prints this message' def enter_command(): cmd = raw_input('Enter command(? for help):') cmd = cmd.strip().lower() return cmd def main(): database = shelve.open('database') # database stores in current directory try: while True: cmd = enter_command() if cmd == 'store': store_person(database) elif cmd == 'lookup': lookup_person(database) elif cmd == '?': print_help() elif cmd == 'quit': return finally: database.close() # Close database in any condition if __name__ == '__main__': main()
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- 詳解Python中如何將數(shù)據(jù)存儲(chǔ)為json格式的文件
- Python 抓取數(shù)據(jù)存儲(chǔ)到Redis中的操作
- Python數(shù)據(jù)存儲(chǔ)之 h5py詳解
- python將類(lèi)似json的數(shù)據(jù)存儲(chǔ)到MySQL中的實(shí)例
- python3爬蟲(chóng)學(xué)習(xí)之?dāng)?shù)據(jù)存儲(chǔ)txt的案例詳解
- 舉例簡(jiǎn)單講解Python中的數(shù)據(jù)存儲(chǔ)模塊shelve的用法
- 將Python中的數(shù)據(jù)存儲(chǔ)到系統(tǒng)本地的簡(jiǎn)單方法
- Python實(shí)現(xiàn)疫情地圖可視化
- python如何繪制疫情圖
- python+selenium 簡(jiǎn)易地疫情信息自動(dòng)打卡簽到功能的實(shí)現(xiàn)代碼
- Python實(shí)現(xiàn)疫情通定時(shí)自動(dòng)填寫(xiě)功能(附代碼)
- Python繪制全球疫情變化地圖的實(shí)例代碼
- Python爬蟲(chóng)爬取全球疫情數(shù)據(jù)并存儲(chǔ)到mysql數(shù)據(jù)庫(kù)的步驟
相關(guān)文章
python開(kāi)發(fā)微信服務(wù)號(hào)消息推送示例
這篇文章主要為大家介紹了python開(kāi)發(fā)微信服務(wù)號(hào)消息推送示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Python3.7將普通圖片(png)轉(zhuǎn)換為SVG圖片格式(網(wǎng)站logo圖標(biāo))動(dòng)起來(lái)
這篇文章主要介紹了Python3.7將普通圖片(png)轉(zhuǎn)換為SVG圖片格式并且讓你的網(wǎng)站Logo(圖標(biāo))從此”動(dòng)”起來(lái),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04用表格幫你了解Python數(shù)據(jù)類(lèi)型
這篇文章主要為大家介紹了Python數(shù)據(jù)類(lèi)型,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>2022-01-01python 接口實(shí)現(xiàn) 供第三方調(diào)用的例子
今天小編就為大家分享一篇python 接口實(shí)現(xiàn) 供第三方調(diào)用的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08python爬蟲(chóng)進(jìn)階之協(xié)程詳解
這篇文章主要介紹了python爬蟲(chóng)進(jìn)階之協(xié)程詳解,coroutine中文翻譯叫協(xié)程,在 Python 中昌指代為協(xié)程對(duì)象類(lèi)型,可以將協(xié)程對(duì)象注冊(cè)到時(shí)間循環(huán)中被調(diào)用,需要的朋友可以參考下2023-08-08python請(qǐng)求域名requests.(url = 地址)報(bào)錯(cuò)
本文主要介紹了python請(qǐng)求域名requests.(url = 地址)報(bào)錯(cuò),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02使用Python實(shí)現(xiàn)為PDF文件添加圖章
在日常工作中,我們經(jīng)常需要給PDF文檔添加一些標(biāo)識(shí),比如公司的圖章或水印圖章,所以本文就來(lái)為大家詳細(xì)介紹一下如何使用Python實(shí)現(xiàn)為PDF文件添加圖章,需要的可以參考下2023-11-11