话不多说,直接上代码,比较神奇的事目前插入 NO 1\3的话,检索时候使用2检索一样能检索到3的内容

#shelve test as databaseimport shelve, sysdef db_insert(db):pid = input('Input a unique number:')person = {}person['name'] = input('input the name:')person['age']  = input('input the age:')person['phone'] = input('input the phone:')db[pid] = personreturndef db_lookup(db):pid = input('The ID you want:')field = input('the field you want:(name, age, phone):')field = field.strip().lower()try:print('the info you want is :', \db[pid][field])returnexcept KeyError:print('key not found')returndef enter_command():cmd = input('Enter command (insert|lookup|quit|? ? for help):')cmd = cmd.strip().lower()return cmddef db_help():print('There are 3 commands:')print('insert: insert a person with id,name,age,phone')print('lookup: find a person with infomation')print('quit: save changes and exit')print('? : get the messages above')returndef main():print('hello this is a shelve test')database = shelve.open('shelve.bat')try:while True:cmd = enter_command()if cmd == '?':db_help()if cmd == 'insert':db_insert(database)if cmd == 'lookup':db_lookup(database)if cmd == 'quit':returnfinally:database.close()if __name__ == '__main__':main()
  • 曾经还问过现在完全看不懂的问题呢

2016年11月21日 北京第一场雪,很美

python shelve相关推荐

  1. python shelve模块_python常用模块之shelve模块

    python常用模块之shelve模块 shelve模块是一个简单的k,v将内存中的数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据类型 我们在上面讲json.pickle ...

  2. python shelve模块_python3 shelve模块的详解

    python3 shelve模块的详解 一.简介 在python3中我们使用json或者pickle持久化数据,能dump多次,但只能load一次,因为先前的数据已经被后面dump的数据覆盖掉了.如果 ...

  3. python shelve模块_说说 Python 的 shelve 模块

    利用 Python 的 shelve 模块, 可以将程序中需要保存的数据,以二进制放入 shelf 文件中.另一个程序就可以读取 shelf 文件,用于业务逻辑.也就是说 shelve 模块可以实现数 ...

  4. Python+shelve读写二进制文件

    Python标准库shelve提供了二进制文件操作的功能,可以像字典一样赋值即可写入文件,也可以像字典一样读取二进制文件,有点类似于NoSQL数据库MongoDB的操作. >>> i ...

  5. python shelve模块_python之shelve模块

    shelve模块 python 专有的序列化模块 只针对文件,用来持久化任意的Python对象 感觉比pickle用起来更简单一些,它也是一个用来持久化Python对象的简单工具.当我们写程序的时候如 ...

  6. python shelve模块_Python中shelve模块

    Python中Shelve模块是对象持久化保存方法,将对象保存到文件里面,缺省(即默认)的数据存储文件是二进制的,可以作为一个简单的数据存储方案.使用时,只需要使用open函数获取一个shelf对象, ...

  7. python shelve模块_python3 shelve模块的实例用法讲解

    python3 shelve模块的详解 python3 shelve模块的详解 一.简介 在python3中我们使用json或者pickle持久化数据,能dump多次,但只能load一次,因为先前的数 ...

  8. python shelve模块_Python中shelve模块的简单介绍(附示例)

    本篇文章给大家带来的内容是关于Python中shelve模块的简单介绍(附示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. shelve:对象持久化的保存的模块,将对象保存到文件 ...

  9. python shelve模块

    shelve很简单,操作跟字典操作一样: 1 import shelve 2 f = shelve.open('sss') # 有文件会读取,没有会创建..shelve是三个文件,后缀名为.bak.d ...

  10. python shelve模块_python之shelve模块详解

    #七.代码示例#1.创建一个shelf对象,直接使用open函数即可 importshelve s= shelve.open('test_shelf.db') #try: s['kk'] = {'in ...

最新文章

  1. 合伙人分开的一点思考
  2. 推荐的 CSS 书写顺序
  3. 史上最详细“截图”搭建Hexo博客——For Windows
  4. Python实现ARP欺骗
  5. Cloud for Customer手机应用上传图片禁止从本地相册选择图片的核心代码
  6. GitLab 11.9 正式发布,自动化工具 ChatOps 已开源
  7. 图片加到json中,提交到服务器端处理异常问题。
  8. Python爬虫系列:判断目标网页编码的几种方法
  9. 阶梯式的岗位技术培训认证体系
  10. Android_之动画1
  11. 网络协议 22 - RPC 协议(下)- 二进制类 RPC 协议
  12. win11打开控制面板闪退怎么办 Windows11控制面板闪退的解决方法
  13. SQL Server高级查询之子查询(在DML语句中使用子查询)
  14. Input and Output-HackerRank C++
  15. 六子棋 c语言程序设计,六子棋计算机博弈系统的研究与实现
  16. c语言高斯法解方程,用C语言实现解线性方程组的高斯消去法
  17. 做正确的事和正确的做事
  18. SpringBoot-Google二步验证
  19. 海思59V200PQtool环境的搭建
  20. 义乌机器人餐厅价格表_义乌一餐厅机器人端盘萌翻吃货

热门文章

  1. lazada2021秋招笔试
  2. 零点工作室暑假集训(牛客练习赛113 )
  3. 开运算、闭运算及其所用
  4. 从复杂适应系统,到自组织理论,再到web2.0的自组织现象
  5. 如何增强宝宝抵抗力和免疫力,宝宝抵抗力差怎么调理
  6. 华为云软件开发平台(DevCloud)流水线式部署Vue项目,并且通过域名访问
  7. 有关C、C++的有用网站
  8. 2021-08-11hit-oslab2系统调用
  9. BPM生态之——端到端流程
  10. 计算机基础知识与运用【基础知识部分】