求助python,关于cpickle。

sinosoul 2009-04-20 05:32:50
各位,我写了个简单的电话本代码,用cpickle存储字典,然后可以查询和删除某条记录(name),但是现在的情况是只能查询到
第一条记录(也就是第一次输入的记录),其它记录都无法查询到,但是在data文件中是可以看到的,请问这是怎么回事?以下是代码:
#!/usr/bin/python
#Filename:addrbook.py

import sys
import time
import cPickle as p

class contact:
def __init__(self,name,phone):
self.name=name
self.phone=phone
list = [self.name,self.phone]
listdicts = {}
listdicts[name] = phone
cont = 'contactlist.data'
f = file(cont,'a')
p.dump(listdicts,f) #dump the objs to file
f.close()
listdicts.clear()
f = file(cont)
print 'checked in %s: ' % p.load(f) #load contactlistfile

def InputInfo():
running = True
while running:
name = raw_input('Enter name:')
phone = raw_input('Enter Phone Number:')
contact(name,phone)
print '''name is: %s and phone is: %s''' % (name,phone)
forsure = raw_input('''Are You Sure?(enter y/n/q):''')

if forsure == 'y':
running = False
contact(name,phone)
sys.exit()
elif forsure == 'q':
break

else:
continue

def SearchInfo():
name = raw_input('Enter name for search:')
cont = file('contactlist.data')
listdicts = p.load(cont)
cont.close()
print listdicts
print 'you want search is %s' % name
if name in listdicts:
print listdicts[name]
else:
print 'not found the name'
sys.exit()

# program 1st check point
if len(sys.argv) < 2:
print 'must specified a command like --add'
print 'sys.argv is %d' % len(sys.argv)
sys.exit()

if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
if option == 'add':
InputInfo()
if option == 'search':
SearchInfo()
else:
print 'no more option,quit job'
sys.exit()

...全文
115 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinosoul 2009-04-22
  • 打赏
  • 举报
回复
谢谢chg_lele,非常感谢!结帖!!!!
chg_lele 2009-04-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chg_lele 的回复:]
cpickle的load次数和dump次数相同,一次只能一条。
def SearchInfo():
name = raw_input('Enter name for search:')
cont = file('contactlist.data')
listdicts = {}
i = 1
while True:
try: #Load times must the same as Dump times
listdicts.update(p.load(cont))
print "Load %s times" % i
print listdicts

[/Quote]
具体内容可以参考一下这个帖子
http://topic.csdn.net/u/20080107/19/a5b84a49-7f1a-409a-b9dd-1bfaf5c3dffd.html
chg_lele 2009-04-21
  • 打赏
  • 举报
回复
cpickle的load次数和dump次数相同,一次只能一条。
def SearchInfo():
name = raw_input('Enter name for search:')
cont = file('contactlist.data')
listdicts = {}
i = 1
while True:
try: #Load times must the same as Dump times
listdicts.update(p.load(cont))
print "Load %s times" % i
print listdicts
i += 1
except EOFError:
print "Load complete"
break
cont.close()
print listdicts
print 'you want search is %s' % name
if name in listdicts:
print listdicts[name]
else:
print 'not found the name'
sys.exit()
sinosoul 2009-04-21
  • 打赏
  • 举报
回复
请帮忙。
iambic 2009-04-20
  • 打赏
  • 举报
回复
建议楼主学习下使用print语句来debug。
sinosoul 2009-04-20
  • 打赏
  • 举报
回复
大家帮帮忙吧。。

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧