欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用shelve模塊實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)存儲(chǔ)的方法

 更新時(shí)間:2015年05月20日 15:46:09   作者:Gavin_dinggengjia  
這篇文章主要介紹了Python使用shelve模塊實(shí)現(xiàn)簡(jiǎn)單數(shù)據(jù)存儲(chǔ)的方法,涉及shelve模塊實(shí)現(xià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ì)有所幫助。

相關(guān)文章

  • 詳解python字符串相關(guān)str

    詳解python字符串相關(guān)str

    這篇文章主要為大家介紹了python字符串相關(guān)str,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • python開(kāi)發(fā)微信服務(wù)號(hào)消息推送示例

    python開(kāi)發(fā)微信服務(wù)號(hào)消息推送示例

    這篇文章主要為大家介紹了python開(kāi)發(fā)微信服務(wù)號(hào)消息推送示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • 如何用用Python將地址標(biāo)記在地圖上

    如何用用Python將地址標(biāo)記在地圖上

    這篇文章主要介紹了如何用用Python將地址標(biāo)記在地圖上,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-02-02
  • python中end=" "的含義及說(shuō)明

    python中end=" "的含義及說(shuō)明

    這篇文章主要介紹了python中end=" "的含義及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Python3.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)

    這篇文章主要介紹了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)型

    這篇文章主要為大家介紹了Python數(shù)據(jù)類(lèi)型,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>
    2022-01-01
  • python 接口實(shí)現(xiàn) 供第三方調(diào)用的例子

    python 接口實(shí)現(xiàn) 供第三方調(diào)用的例子

    今天小編就為大家分享一篇python 接口實(shí)現(xiàn) 供第三方調(diào)用的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • python爬蟲(chóng)進(jìn)階之協(xié)程詳解

    python爬蟲(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-08
  • python請(qǐng)求域名requests.(url = 地址)報(bào)錯(cuò)

    python請(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文件添加圖章

    使用Python實(shí)現(xiàn)為PDF文件添加圖章

    在日常工作中,我們經(jīng)常需要給PDF文檔添加一些標(biāo)識(shí),比如公司的圖章或水印圖章,所以本文就來(lái)為大家詳細(xì)介紹一下如何使用Python實(shí)現(xiàn)為PDF文件添加圖章,需要的可以參考下
    2023-11-11

最新評(píng)論