Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从文本文件分配set/get方法_Python - Fatal编程技术网

Python 从文本文件分配set/get方法

Python 从文本文件分配set/get方法,python,Python,我对Python比较陌生(大约5个月),请原谅我的无知。我的任务是创建一个程序来做一些事情 首先,我有一个文本文件,其中存储的信息如下: 姓名、街道、市州邮编、生日、宠物 第二,我的指示包括: 读取文本文件并确定年龄,然后根据年龄确定折扣。例如,如果年龄在18-25岁,他们可以得到5%的折扣 将宠物偏好与供应商匹配 我正在为每个属性使用类。我有一个单独的文件,用于下面所示的课程信息。我已为列出的每个属性设置/获取方法 从information.py class GetInfo: de

我对Python比较陌生(大约5个月),请原谅我的无知。我的任务是创建一个程序来做一些事情

首先,我有一个文本文件,其中存储的信息如下:

姓名、街道、市州邮编、生日、宠物

第二,我的指示包括:

  • 读取文本文件并确定年龄,然后根据年龄确定折扣。例如,如果年龄在18-25岁,他们可以得到5%的折扣
  • 将宠物偏好与供应商匹配
我正在为每个属性使用类。我有一个单独的文件,用于下面所示的课程信息。我已为列出的每个属性设置/获取方法

从information.py

class GetInfo:
    def __init__(self,name,street,city,state,zipcode,birthday,pet):
        self.__name = name

    def set_name(self,name):
        self.__name = name

    def get_name(self):
        return self.__name
最后我必须为每个人写一封信。我不知道如何将数据分开,以便每个人都写这封信。我将使用.write(str)方法写入每一行。以下是我目前的主要计划:

import information
import os
import time
CURRENT_YEAR = 2018

def main():
    customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
    tempFile = open('temp.txt','w')
    with open('c.TXT') as customerList:
    lines = customerList.readlines()
    for line in lines:
        tempFile.write( str(GetInfo.name) + '\n')
    tempFile.close()
main()
对于供应商列表,我将有一个字典,例如:

vendors = {"Cat:CatCity"}
我对下一步感到困惑,同时在尝试运行程序时也收到此错误:

"main.py", line 10, in main customerInfo = information.GetInfo(name,street,city,state,zipcode,birthday,pet)
NameError: name 'name' is not defined
尝试以下课程:

class GetInfo:
    def __init__(self,name,street,city,state,zipcode,birthday,pet):
        self.inner_name = name

    @property
    def get_name(self):
        return self.inner_name

    @name.setter
    def set_name(self, name):
        self.inner_name = name
您不需要使用getter和setter。在python中,您可以访问对象的所有属性,而不需要它们。另见:

代码中的实际问题是行

tempFile.write( str(GetInfo.name) + '\n')
对象
GetInfo
不是类的实例,而是类本身。并且该类没有属性
name
。你的意思可能是:

tempFile.write( str(customerInfo.name) + '\n')

set\u name
get\u name
是完全无用的功能。只需使用普通属性访问。巨蟒!=JAVA另外,除非您想要双下划线名称损坏,否则不要使用双下划线名称损坏。当然,您的错误非常简单,
name
没有在任何地方定义。不,
name=def get\u name()
是一个语法错误。无论如何,您的
get\u name
函数是不需要的,我不知道您希望它们在这方面做什么case@Torxed事实上,这不会,因为他们使用了双下划线名称。但他们不应该这么做。所以,它应该是
.name
,一直到这个问题没有任何意义为止。您不需要将这些函数分配给变量。事实上,这些函数根本不这样做,它们分配给实例属性。但是你也不需要他们这样做。删除完全无用的
属性
方法同样,问题不是AttributeError,而是NameError,因为当他们初始化
GetInfo
实例时,他们会传递一组未定义的变量(
name
只是第一个变量)谢谢你的建议。我还没有更改该类,但我确实尝试将其更改为customerInfo,并收到了相同的错误。我需要为类文件或主程序中的某些内容指定名称吗?