我正在尝试创建一个在循环遍历文本文件时使用变量值的类

问题描述 投票:1回答:2

''''我正在打开一个文本文件,并在文件的各行中循环尝试提取信息。如果某行以地址开头,则我将“地址”后面的所有内容都切成该行,那么文件中将有多个地址,并且每个地址都具有诸如-家庭或办公室,居住地代码和注册日期之类的规范。我想存储一个具有所有属性的名为Address1的对象,然后移至下一个Address并将它们存储为具有不同名称的对象我收到一个错误消息,变量Addr,Cate,Resi,date未定义。我在哪里错了,请帮忙。我是python的新手。'''

handle = open('cibil1p2.txt')
class Adress_data:

    def __init__(self , Address , Category ,Residence_code , Date_Reported):
        self.Address = Address
        self.Category = Category
        self.Residence_code = Residence_code
        self.Date_Reported = Date_Reported
        print(self.Address , self.Category , self.Residence_code , self.Date_Reported)
    def A_data1(self):
        printing = False
        for line in handle:
            line =line.strip()

            if line.startswith('ADDRESS(ES):'):
                printing = True
                continue
            elif line.startswith('EMPLOYMENT INFORMATION :'):
                printing = False
                        # print(line)
                break
            elif printing:
                line.lstrip()
                if line.startswith("ADDRESS:" ):
                    Addr=line[8:]
                elif line.startswith("CATEGORY:" ):
                    Cate=line[9:]   
                elif line.startswith("RESIDENCE CODE:" ):
                    Resi=line[15:]
                if line.startswith("DATE REPORTED:" ):
                    Date=line[14:]   
                    print(line)                    
p1 = Adress_data(Addr, Cate , Resi , Date)                      
p1.A_data1

谢谢

python loops class object text-files
2个回答
0
投票

当您在line下方调用时,您尚未定义它们。p1 = Adress_data(Addr,Cate,Resi,Date)


0
投票

确定]

因此,现实是失败的行如下。

p1 = Adress_data(Addr, Cate , Resi , Date) 

这是因为在给定的代码示例中未定义变量“ Addr,Cate,Resi和Date”。

您的班级定义要求这些都存在。

def __init__(self , Address , Category ,Residence_code , Date_Reported):

这意味着您需要将它们从某个地方传递到您的类中,或者让您的类从某个地方读取它们,即您的开放式声明,这将是两个选择。

从表面上看,您的问题似乎很清楚,但是我的直觉告诉我问题不完整,或者没有提供给我们可以帮助我们弥合您所看到的鸿沟的上下文。

编辑1

阅读注释并更深入地查看代码后,也许您可​​以尝试以下方法。

class Adress_data(object):

    def __init__(self, Address, Category, Residence_code, Date_Reported):
        self.Address = Address
        self.Category = Category
        self.Residence_code = Residence_code
        self.Date_Reported = Date_Reported
        print(self.Address, self.Category, self.Residence_code, self.Date_Reported)

    @staticmethod
    def A_data1():
        printing = False

        Addr = None
        Cate = None
        Resi = None
        Date = None

        with open('cibil1p2.txt') as handle:

            for line in handle.readlines():
                line = line.strip()

                if line.startswith('ADDRESS(ES):'):
                    printing = True
                    continue
                elif line.startswith('EMPLOYMENT INFORMATION :'):
                    printing = False
                    # print(line)
                    break
                elif printing:
                    line.lstrip()
                    if line.startswith("ADDRESS:"):
                        Addr = line[8:]
                    elif line.startswith("CATEGORY:"):
                        Cate = line[9:]
                    elif line.startswith("RESIDENCE CODE:"):
                        Resi = line[15:]
                    if line.startswith("DATE REPORTED:"):
                        Date = line[14:]
                        print(line)

        return (Addr, Cate, Resi, Date)


    p1 = Adress_data(*Adress_data.A_data1())
© www.soinside.com 2019 - 2024. All rights reserved.