Python入门教程中的联系簿控制台小程序
最近学习了下Pyhton,入门教程中有个联系簿的小程序。我用的是3.2的,有些地方和老版本不一样,比如print什么的。存储的话,就是用的教程中的pickle,
大家只要改下datafile,保证文本文件存在即可,运行开始先init,给文本文件查一行数据。欢迎大家交流,
由于python对缩进要求很严,所以代码大家要重新弄下(本人一直在用UltraEdit,感觉蛮好的)。
#Author:zhuqiang
#Email:zhuqiang4433@gmail.com
#FileName:MyAddrList.py
import sys
import pickle as p
datafile='D:\python\data\personList.data'
#shoplistfile='e:\python\data\shoplist.data'
class Person:
def __init__(self,name,age,address,email):
self.name=name
self.age=age
self.address=address
self.email=email
def isHave(name):
try:
returnValue=False
perList=[]
f=open(datafile,'rb')
perList=p.load(f)
#print(perList)
for item in perList:
if item.split('|')[0]==name:
returnValue=True
break
if returnValue:
print('Person %s have in data' % name)
else:
print('Person %s is not in data' % name)
return returnValue
finally:
f.close()
def getPersonList():
'''To get the person list'''
try:
f=open(datafile,'rb')
personList=p.load(f)
return personList
finally:
f.close()
def getPersonListPrint():
'''To get the person list'''
try:
f=open(datafile,'rb')
personList=p.load(f)
i=0
for item in personList:
i+=1
print(i,',',item)
finally:
f.close()
def test1():
try:
f=open(shoplistfile,'rb')
storedlist=p.load(f)
return storedlist
finally:
f.close()
def inputStr(s):
str1=''
while True:
print('input something for ',s)
str1=input(':')
if len(str1)>0:
break
return str1
def init():
personList=['zhu4433|11|china|email']
#print(getPersonList())
f=open(datafile,'wb')
p.dump(personList,f)
f.close()
print('complet init')
def addPerson():
try:
name=inputStr('name')
while True:
if isHave(name):
print(name,' is in data,try another name')
name=inputStr('name')
else:
break
age=inputStr('age')
address=inputStr('address')
email=inputStr('Email')
personList=getPersonList()
f=open(datafile,'wb')
person=Person(name,age,address,email)
personList.append(person.name+'|'+person.age+'|'+person.address+'|'+person.email)
p.dump(personList,f)
print('add completed')
finally:
f.close()
def deletePerson():
try:
name=input('please input the person name which you wount to delete:')
perList=getPersonList()
f=open(datafile,'wb')
for item in perList:
if item.split('|')[0]==name:
perList.remove(item)
break
p.dump(perList,f)
print(perList)
finally:
f.close()
def findPerson():
try:
name=input('please input the person name which you wount to find:\n')
flag=False
f=open(datafile,'rb')
perList=p.load(f)
for item in perList:
if item.split('|')[0]==name:
flag=True
print('Person: ',name,' is in data.The detail is:\n',item)
break
if flag==False:
print('Person: ',name,' is not in data.\n')
finally:
f.close()
if len(sys.argv)>=2 and sys.argv[1].startswith('--'):
option=sys.argv[1][2:]
if option=='version':
print('Version 1.0')
#print(sys.argv[0])
elif option=='help':
print('''\
This program record person massage.
Options include:
--version:Prints the version number
--help :Display this help''')
else:
print('Unknow option.')
sys.exit()
elif len(sys.argv)>=2 and sys.argv[1]=='init':
init()
else:
print('Walcom to use this System!\ncode h for help')
while True:
code=input('Please input the code:\n')
if code=='quit':
break
elif code=='h':
print('''\
1,isHave---To know is person name is in the data
2,add---Add a new person in to the data
3,del---Delete a person
4,init--initialization data
5,f--find the person
6,fall--get the details for all person
''')
elif code=='init':
init()
elif code=='isHave':
name=input('input a name:\n')
isHave(name)
elif code=='add':
print('add a person')
addPerson()
elif code=='del':
deletePerson()
#print('delete a person')
elif code=='f':
findPerson()
elif code=='fall':
getPersonListPrint()
else:
print('Unknow code.Please input another code.')
print('Thanks for you using.')
sys.exit()
浙公网安备 33010602011771号