Python类中未定义的OOP变量-需要帮助了解__init__

时间:2018-08-03 20:24:23

标签: python python-3.x oop

我是OOP的新手,我一直在尝试编写一个我可以导入的类,这将有助于我解析文件。我意识到我不需要上一堂课来做,但是我想我会尽力让我开始熟悉OOP。

此代码有效

import re
import os

destdir = r"FilePathToDirectory"

class Parsey():                            

    def GetNums(self,source, destination, trim = True):
        with open (os.path.join(destdir,source), 'r') as infile:
            with open (os.path.join(destdir,destination), 'w') as outfile:
                for line in infile:
                    #Look for number patern match
                    if re.match(r'(.*)\d\d-\d\d-\d\d\d\d(.*)', line):
                        #If trim is True clean up the line
                        if trim == True:
                            #Find the first numeric character
                            firstdig = re.search("\d",line)
                            #Set the firstdig variable to the integer of that index
                            firstdig = firstdig.start()
                            #Set the line equal to only the begining and ending indexes of the number
                            line=line[firstdig:firstdig+10]
                            #Remove the dashes from the string
                            line = line.replace('-','')
                            outfile.writelines(line+'\n')
                        else:
                            outfile.writelines(line)

该代码没有,我不确定为什么没有。

import re
import os

class Parsey():

    def __init__(self, destdir=''):
        self.destdir = r"FilePathToDirectory"

    def GetNums(self,source, destination, trim = True):
        with open (os.path.join(destdir,source), 'r') as infile:
            with open (os.path.join(destdir,destination), 'w') as outfile:
                for line in infile:
                    #Look for number patern match
                    if re.match(r'(.*)\d\d-\d\d-\d\d\d\d(.*)', line):
                        #If trim is True clean up the line
                        if trim == True:
                            #Find the first numeric character
                            firstdig = re.search("\d",line)
                            #Set the firstdig variable to the integer of that index
                            firstdig = firstdig.start()
                            #Set the line equal to only the begining and ending indexes of the number
                            line=line[firstdig:firstdig+11]
                            #Remove the dashes from the string
                            line = line.replace('-','')
                            outfile.writelines(line+'\n')
                        else:
                            outfile.writelines(line)

我收到错误: GetNums中的第10行     打开(os.path.join(destdir,source),'r')作为文件名: NameError:名称“ destdir”未定义

据我了解,类对象的名称空间将允许类中的函数查看该类中声明的所有变量。

2 个答案:

答案 0 :(得分:1)

您需要将第10行更改为:

with open (os.path.join(self.destdir, destination), 'w') as outfile:

在您的情况下,Python首先在testdir内查找GetNums,如果在该位置找不到它,它将在模块中寻找该名称。它不会神奇地使用tesdir中的__init__。名称self代表您稍后将创建的实例。因此,在__init__中,您基本上设置了mysinstance.testdir,随后在GetNums中,您可以使用mysinstance.testdir进行访问。 self只是mysinstance(即您稍后创建的实例)的占位符。

您可以在documentation中阅读详细信息。

答案 1 :(得分:0)

@MikeMüller钉牢了它,但这是完整的更正代码。

import re
import os

class Parsey():

    def __init__(self, destdir=''):
        self.destdir = r"FilePathToDirectory"

    def GetNums(self,source, destination, trim = True):
        with open (os.path.join(self.destdir,source), 'r') as infile:
            with open (os.path.join(self.destdir,destination), 'w') as outfile:
                for line in infile:
                    #Look for number patern match
                    if re.match(r'(.*)\d\d-\d\d-\d\d\d\d(.*)', line):
                        #If trim is True clean up the line
                        if trim == True:
                            #Find the first numeric character
                            firstdig = re.search("\d",line)
                            #Set the firstdig variable to the integer of that index
                            firstdig = firstdig.start()
                            #Set the line equal to only the begining and ending indexes of the number
                            line=line[firstdig:firstdig+10]
                            #Remove the dashes from the string
                            line = line.replace('-','')
                            outfile.writelines(line+'\n')
                        else:
                            outfile.writelines(line)