搜索
有爱,有技术,有你^_^)y
╱人◕‿‿◕人╲订下契约(注册新用户)

合作站点账号登陆

QQ登录

只需一步,快速开始

快捷导航
查看: 427|回复: 1
收起左侧

在信息储存上有点问题解决不了

[复制链接]

该用户从未签到

18

主题

44

好友

1万

积分

第一章

积分
11039
发表于 2014-7-11 02:10:29 | 显示全部楼层 |阅读模式

╱人◕‿‿◕人╲定下契约

您需要 登录 才可以下载或查看,没有账号?╱人◕‿‿◕人╲订下契约(注册新用户)

x
本帖最后由 绯色の胖子 于 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
签名被小宅喵吞掉了~~~~(>_<)~~~~
回复

使用道具 举报

该用户从未签到

3

主题

11

好友

2178

积分

Continue

积分
2178
发表于 2014-7-17 10:27:02 | 显示全部楼层
本帖最后由 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) 这个函数.

评分

参与人数 1宅币 +5 收起 理由
我叫粉笔头的说 + 5 感谢回答

查看全部评分

签名被小宅喵吞掉了~~~~(>_<)~~~~
回复 支持 反对

使用道具 举报

本版积分规则

小黑屋|手机版|技术宅(Z站|基宅) ( 粤ICP备18082987号-1 )

GMT+8, 2025-6-16 20:25 , Processed in 0.096063 second(s), 23 queries , Redis On.

Copyright © 2018 技术宅社区

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表