- UID
- 74570
- 在线时间
- 0 小时
- 最后登录
- 2016-11-2
- 注册时间
- 2012-3-8
- 宅魂
- 836 点
- 贡献
- 135 点
- 宅币
- 7276 枚
- 灵石
- 0 块
- 元气(技能点)
- 17 点
- 活跃
- 0 ℃
- 听众
- 4
- 收听
- 0
该用户从未签到
第一章
- 积分
- 11039
|
本帖最后由 绯色の胖子 于 2015-6-12 18:13 编辑
在看完简明python后尝试着做最后的那个通讯录
在信息储存上有点问题解决不了
#!/usr/bin/python
#Filename : Directory inquirles
import cPickle as p
class Person_Address:
def __init__(self,name,group,phone_number,email_address):
self.name = name
self.group = group
self.phone_number = phone_number
self.email_address = email_address
def Print_message(self):
'''It is a message print function in class
print a person's name,group,phone number,email address'''
print self.name,self.group,self.phone_number,self.email_address
direct = {}
Storedfile = '/home/licht/Documents/MessageBook'
def add_new():
'''It is a new person message append function'''
direct['licht'] = Person_Address('licht','family','132','3628')
name = raw_input("Please inpute the person's name:")
group = raw_input("Please inpute the person's group:")
number = raw_input("Please inpute the person's phone numbr:")
email = raw_input("Please inpute the person's email address:")
direct[name] = Person_Address(name,group,number,email) #这里,类被存储到字典里时是存成
{'licht':<__main__.Person_Address instance at 0x20d6680>}
f_temp = file(Storedfile,'ab')
p.dump(direct,f_temp)
f_temp.close()
def search_one():
'''It is a person message search function'''
name = raw_input("Please inpute the person's name:")
f_temp = file(Storedfile,'rb')
while True:
try:
store = p.load(f_temp) #在这里被取出来后还是{'licht': <__main__.Person_Address instance at 0x20d6680>},怎么能才能让它取出以后是可以正常输出信息?
if store.has_key(name):
print store
break
except EOFError:
break
if __name__ == '__main__':
add_new()
search_one()
还求大神赐教!
更多 0 |
|