#!/usr/bin/python
#Filename: cpickle.py import cPickle as p
import os shoplistfile="shoplist.data"
storedlist={} def menu():
running=True
try:
while running:
try:
f=file(shoplistfile)
storedlist=p.load(f)
command=raw_input("Please input command: ")
if command=="help":
help()
elif command=="add":
add(storedlist)
elif command=="delete":
delete(storedlist)
elif command=="update":
update(storedlist)
elif command=="select":
select(storedlist)
elif command=="quit":
running=False
elif command=='show':
show(storedlist)
else:
print "ERROR, please input help!"
finally:
f.close()
else:
print "Done"
except EOFError:
print "QUIT"
except KeyboardInterrupt:
print "Q"
finally:
print "DONE" def show(storedlist):
print '-'*40
for (k,v) in storedlist.items():
print '%s \t'%k,v
print '-'*40 def add(storedlist):
try:
username=raw_input("Please input add username:")
# print storedlist
# print username
if(storedlist.has_key(username)):
print "Username Exist!!"
return 0
emailbox=raw_input("Please input add email:")
storedlist[username]=emailbox
except:
print "err add"
finally:
write(storedlist) def select(storedlist):
try:
username=raw_input("Please input select username:")
if storedlist.has_key(username):
print storedlist[username]
else:
print 'Username not find!!'
return 0
except:
print "err select"
finally:
write(storedlist) def delete(storedlist):
try:
username=raw_input("Please input delete username:")
if storedlist.has_key(username):
print storedlist[username]
else:
print 'Username not find!!'
return 0
storedlist.pop(username)
except:
print "err delete"
finally:
write(storedlist) def update(storedlist):
try:
#not key ?
username=raw_input("Please input update username:")
if storedlist.has_key(username):
print storedlist[username]
else:
print 'Username not find!!'
return 0
emailbox=raw_input("Please input update email:")
storedlist[username]=emailbox
except:
print "err update"
finally:
write(storedlist) def write(stored):
f=file(shoplistfile,'w')
p.dump(stored,f)
f.close() def index():
if os.path.exists(shoplistfile):
menu()
else:
print 'Contacts not find!'
print 'Create Contacts...'
create_con() def create_con():
shoplist={}
try:
f=file(shoplistfile,'w')
p.dump(shoplist,f)
except IOError:
print 'find not file!!'
finally:
f.close()
menu() def help():
"""
------------------------
==>>help
==>>add < username email >
==>>delete < username >
==>>update < username newemail >
==>>select < username >
==>>quit
------------------------"""
print help.__doc__ if __name__=='__main__':
index()

练习字典,看简明的python后面有一个练习,根据cPickle 写一个通讯录,恶心下自己!

还没有写完准备弡上cpickle 还有字典的更多相关文章

  1. 前端性能测试工具 : dynaTrace Ajax (还没写完)

    今天开始写这个工具, #什么是dynaTrace Ajax? 随着 jQuery.Dojo.YUI 等框架的兴起让构建 Web2.0 应用更加容易,但随之带来的定位等应用问题也越来越难,尤其是与性能相 ...

  2. XObject.java 对象还没写完,希望电脑不会丢失。坏笑,早点见。

    /*面向对象强调的是对象, 面向过程强调的是功能行为,打开行为,关闭行为,执行行为,把多个行为封装成对象执行更强大的功能就是面向对象,是把多个函数, 多 个行为封装在一起,单一的函数执行对象的功能太困 ...

  3. 【creo】CREO5.0+VS2019配置(还没写完)

    欢迎大家一起学习使用c++对CREO5.0二次开发. 第1步,建立开发目录:在E盘(或者其他盘)新建creo_cpp文件夹,文件夹中新建ABC_TOOLS用来存放我们开发的工具,CODE文件夹存放开发 ...

  4. HDU 4640 状态压缩DP 未写完

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4640 解题思路: 首先用一个简单的2^n*n的dp可以求出一个人访问一个给定状态的最小花费,因为这i个 ...

  5. webuploader 跨域上传demo(还没有写记录一下)

    webuploader 跨域上传demo(还没有写记录一下)

  6. 写完代码就去吃饺子|The 10th Henan Polytechnic University Programming Contest

    河南理工大学第十届校赛 很久没有组队打比赛了,好吧应该说很久没有写题了, 三个人一起玩果然比一个人玩有趣多了... 前100分钟过了4题,中途挂机100分钟也不知道什么原因,可能是因为到饭点太饿了?, ...

  7. 缓存服务,还未创建完缓存时, 需要更改图层名称、服务名称、数据源位置、mxd名称等

    缓存服务,还未创建完缓存时, 需要更改图层名称.服务名称.数据源位置.mxd名称等.已经创建好的缓存还可以再用吗? 测试后可以, 注意:新服务相对旧服务,符号样式没有改变,切片方案没有变化. 测试步骤 ...

  8. 用putty玩linux的时候由于以前用window 习惯写完东西按一下ctrl+s 保存

    问题描述:用putty玩linux的时候由于以前用window 习惯写完东西按一下ctrl+s 保存,但是在putty一按下就不能再输入了.后来查找到:ctrl+s 是putty的一个命令大概是这样子 ...

  9. 不写完不让回家的JQuery的事件与动画

    在这看不见太阳的小黑屋里,苦逼的一天又开始了 好了闲话我也就不扯了,接下来我就来说说我对jQuery事件和动画的理解吧!!! 还是得再扯两句,我们敬爱的,Y老师讲完了,jQuery事件和动画,对着我们 ...

随机推荐

  1. SQL Server 2008中SQL增强之一:Values新用途 001

    连接集合 select '1' as id,'wang' as name union select '2' as id,'admin' as name 现在可以这么写了 select id,name ...

  2. Java 中的锁

    Java中的锁分类 在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类.介绍的内容如下: 公平锁/非公平锁 可重入锁 独享锁/共享锁 互斥锁/读写锁 乐观锁/悲观锁 分 ...

  3. jxls2 java.lang.NegativeArraySizeException

    某个单元格使用jx:each报这个错误. 内部代码,创建一个二维数组时,传了一个负值进去 解决办法 将这个单元格删除,重新编写批注

  4. VS2013 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array<int>::operator [](int)”只是在返回类型上不同

    1,VS2013 错误 1 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array ...

  5. Example of Formalising a Grammar for use with Lex & Yacc

    Here is a sample of a data-file that we want to try and recognise. It is a list of students and info ...

  6. 7. Reverse Integer (整数的溢出)

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 For the p ...

  7. 【转】将项目打成war包并用tomcat部署的方法,步骤及注意点

    部署的遇到第一个问题,就是tomcat和jdk的环境问题: 首先 理解为啥要关注这二者的环境呢?他们还是有关系的–tomcat 作为比较流行的java Web服务器也是用java来实现的一个比较大的软 ...

  8. 网页中flash设置

    我们现在大部分人做网页,都是直接用DW插入flash,而且DW也是所见即所得,直接生成了相应的flash显示代码.可是我们又有多少人了解这些直接由DW生成的代码呢?其实我接触flash player标 ...

  9. Delphi: 圆形进度(环形进度)

    起源: 重回DC5项目,资源下载美工提供圆形进度条,复习Delphi,为实现其颇觉有趣,遂研究其. 最终效果图如下: 实现: 制作TCircleProgress控件,实现方法参照系统之TGauge控件 ...

  10. 项目总结01:JSP mysql SpringMvc下中国省市县三级联动下拉框

    JSP mysql SpringMvc下中国省市县三级联动下拉框 关键词 JSP  mysql数据库  SpringMvc  ajax   Controller层  Service层  中国地区  省 ...