暮零天晨
在信息储存上有点问题解决不了

本帖最后由 绯色の胖子 于 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

neuront
本帖最后由
展开Biu

本帖最后由 neuront 于 2014-7-17 18:06 编辑

楼主你所谓的「正常输出信息」是不是指在

[mw_shl_code=python,true]person = Person_Address(...)

print person # 这里[/mw_shl_code]

输出的时候产生的内容是定义在 Print_message 里的那种, 而不是一串 <__main__.Person_Address instance at 0x20d6680> 这样的东西? 如果是这样的话你需要覆盖 __str__(self) 这个函数.

[查看全文]