早教吧 育儿知识 作业答案 考试题库 百科 知识分享

简明python后面的作业电话簿求问importpickleasplinkmanlistfile='Linkmanlist.data'classperson:definit(self,city,phonenumber):self.city=cityself.phonenumber=phonenumberdefsetcity(self,city):self.city=citydefsetphonenum

题目详情
简明python后面的作业电话簿求问
import pickle as p
linkmanlistfile = 'Linkmanlist.data'
class person:
def __init__(self,city,phonenumber):
self.city = city
self.phonenumber = phonenumber
def setcity(self,city):
self.city = city
def setphonenumber(self,phonenumber):
self.phonenumber = phonenumber
def getcity(self):
return self.city
def getphonenumber(self):
return self.phonenumber
try :
f = open(linkmanlistfile,'r')
linkmanlist = p.load(f)
except :
f = open(linkmanlistfile,'w')
linkmanlist = {}
p.dump(linkmanlist,f)
f.close()
while True:
print('Please Enter Your Choice:')
print('1.Add a linkman' )
print('2.Edit a linkman' )
print('3.Delete a linkman')
print('4.Look for a linkman')
print('5.Print the whole linkman list')
print('6.Exit')
choice=input('Now make your choice:')
print(' ')
if choice == '1':
thename = raw_input("Enter your new linkman's name:")
thename = thename.replace(' ','_')
if thename in linkmanlist.iterkeys():
print('Already include this people!')
else :
thecity = raw_input("Enter your new linkman's livecity:")
thecity = thecity.replace(' ','_')
thenum = raw_input("Enter your new linkman's phonenumber:")
f = file(linkmanlistfile,'r')
linkmanlist = p.load(f)
linkmanlist[thename] = person (thecity,thenum)
f = file(linkmanlistfile,'w')
p.dump(linkmanlist,f)
f.close()
print(' ')
if choice=='2':
thename = raw_input("Enter the one you want to edit linkman's name:")
thename = thename.replace(' ','_')
if thename in linkmanlist.iterkeys():
f = file(linkmanlistfile,'r')
linkmanlist = p.load(f)
n = 1
while n == 1:
print('Please choice what you want edit:')
print('1.Name')
print('2.City')
print('3.Phonenumber')
print('4.Exit')
对了我用的是python3.3
报错
Traceback (most recent call last):
File "E:/python/score.py",line 17,in
linkmanlist = p.load(f)
TypeError:'str' does not support the buffer interface
During handling of the above exception,another exception occurred:
Traceback (most recent call last):
File "E:/python/score.py",line 21,in
p.dump(linkmanlist,f)
TypeError:must be str,not bytes
▼优质解答
答案和解析
注意看文档,文档中文明写了
”an on-disk file opened for binary reading“
也就是说你用open打开文件的时候,读需要用“rb”,写需要用“wb”