从字典保存到文件(错误)

时间:2017-06-13 13:06:04

标签: python

我正在参加我的第一个Python课程,所以请耐心等待我,我在编程方面有零经验,但我非常渴望学习。如果你能引导我朝着正确的方向前进,我真的很感激。提前谢谢。

我已经查看过以前的问题,但我找不到一个完全帮助/解释我陷入困境的地方。我有一个存储团队成员(姓名,电话,球衣)的字典,需要能够将其写入文件。以下是我目前所拥有的,当我运行此操作时,我收到错误AttributeError:'dict' object has no attribute 'getname'

class Member:
    def get name(self):
        return self.name
    def get phone(self):
        return self.phone
    def get jersey(self):
        return self.jersey

members={}
def saveData(members, filename):
    filename=input("Filename to save:")
    outFile=open(filename,"wt")
    for x in members.keys():
        name=members[x].getname
        phone=members[x].getphone
        jersey=members[x].getjersey
        outFile.write(name+","+phone","+jersey+"\n")
    print("Data Saved")
    outFile.close() 

2 个答案:

答案 0 :(得分:1)

会员班

你在函数名中加了一个空格,所以它不起作用。 你似乎没有__init__功能。

class Member:
    def __init__(self, name, phone, jersey):
        self.name = name
        self.phone = phone
        self.jersey = jersey
    def get_name(self):
        return self.name
    def get_phone(self):
        return self.phone
    def get_jersey(self):
        return self.jersey

无论如何,只是不要让这些功能变得更容易;用户可以使用点语法获取类的变量。

class Member:
    def __init__(self, name, phone, jersey):
        self.name = name
        self.phone = phone
        self.jersey = jersey

壳:

>>> member1 = Member("Dave", "123456789", "red")
>>> member.name
'Dave'
>>> member.phone
'123456789'
>>> member.jersey
'red'

saveData函数

它不起作用,你应该这样做:

def saveData(members):           # don't include filename, it's going to be redefined later
    filename = input("Filename to save: ") # space at the end
    with open(filename, 'wt') as outFile:  # using with for files is recommended
                                           # then you don't need to close the file
      for x in members:            # you can directly iterate from a dict
          name = x.get_name()    # you didn't call the function at all
          phone = x.get_phone()  # members[x] not necessary
          jersey = x.get_jersey()
          outFile.write(name+", "+phone+", "+jersey+"\n") #missing + sign
      print("Data Saved")

工作示例

<强> __初始化__。PY

class Member:
    def __init__(self, name, phone, jersey):
        self.name = name
        self.phone = phone
        self.jersey = jersey
    def get_name(self):
        return self.name
    def get_phone(self):
        return self.phone
    def get_jersey(self):
        return self.jersey

def saveData(members):
    filename = input("Filename to save: ")
    with open(filename, 'wt') as outFile:  

      for x in members:            
          name = x.get_name()    
          phone = x.get_phone()  
          jersey = x.get_jersey()
          outFile.write(name+", "+phone+", "+jersey+"\n")
      print("Data Saved")

IDLE shell

>>> members = [Member("Dave", "123456789", "red"),
           Member("Tom", "133742097", "yellow"),
           Member("Elisa", "122333444", "blue"),
           Member("John", "987654321", "blue")
          ]
>>> saveData(members)
Filename to save: output.txt
Data Saved

<强> output.txt的

Dave, 123456789, red
Tom, 133742097, yellow
Elisa, 122333444, blue
John, 987654321, blue

答案 1 :(得分:0)

您可以在Member类中定义getname,getphone,如下所示:

class Member:
    def getname(self):
        return self.name
    def getphone(self):
        return self.phone
    def getjersey(self):
        return self.jersey

然后,您可以在 saveData 函数中获取getter的值:

name=members[x].getname()
phone=members[x].getphone()
jersey=members[x].getjersey()