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

Posted

技术标签:

【中文标题】从字典保存到文件(错误)【英文标题】:Saving from dictionary to file (error) 【发布时间】:2017-06-13 13:06:03 【问题描述】:

我正在上我的第一堂 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() 

【问题讨论】:

你能告诉我什么是会员吗? 这不可能是你的真实代码。 def get name(self): 抛出语法错误。如果不知道members 是什么,我们真的无能为力。 【参考方案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

不管怎样,只要不做这些 get 函数就容易多了;用户可以使用点语法获取类的变量。

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'

保存数据函数

这不行,你应该这样做:

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")

工作示例

__init__.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")

空闲外壳

>>> 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

【讨论】:

【参考方案2】:

您可以在您的 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()

【讨论】:

以上是关于从字典保存到文件(错误)的主要内容,如果未能解决你的问题,请参考以下文章

从python字典如何将键和值保存到* .txt文件[关闭]

如何将字典列表保存到文件中?

从 UIImagePickerController 保存视频

如何将字典保存到文件中,保持良好的格式?

如何将字典保存到文件中?

目标C将带有扩展名和名称的文件保存到字典