最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)文章

最新評(píng)論

威信县| 鱼台县| 红安县| 水城县| 辽宁省| 长垣县| 南华县| 图片| 疏附县| 娄烦县| 内黄县| 柳州市| 沙田区| 葵青区| 噶尔县| 岑巩县| 湄潭县| 邵阳市| 淄博市| 通辽市| 三都| 当雄县| 南岸区| 商城县| 睢宁县| 乡宁县| 康马县| 胶州市| 左权县| 西青区| 綦江县| 乌鲁木齐市| 惠州市| 都兰县| 大方县| 榆树市| 弥渡县| 峨眉山市| 鲁甸县| 潜江市| 普兰县|