国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python使用shelve模塊實現簡單數據存儲的方法

2019-11-25 17:25:29
字體:
來源:轉載
供稿:網友

本文實例講述了Python使用shelve模塊實現簡單數據存儲的方法。分享給大家供大家參考。具體分析如下:

Python的shelve模塊提供了一種簡單的數據存儲方案,以dict(字典)的形式來操作數據。

#!/usr/bin/pythonimport sys, shelvedef 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] = persondef 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 cmddef 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 conditionif __name__ == '__main__':  main()

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台前县| 丘北县| 兴仁县| 凤翔县| 连江县| 沐川县| 东乡县| 安岳县| 泗洪县| 开封市| 伊吾县| 伊金霍洛旗| 女性| 宁远县| 梁平县| 电白县| 江安县| 百色市| 广河县| 杂多县| 南城县| 进贤县| 定州市| 揭东县| 三河市| 临颍县| 如东县| 南溪县| 马龙县| 张家港市| 武鸣县| 洛浦县| 巨鹿县| 娱乐| 江华| 温泉县| 鹰潭市| 大新县| 宜川县| 海安县| 龙泉市|