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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (82)

我是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)

我收到错误:第10行,在GetNums中打开(os.path.join(destdir,source),'r')为infile:NameError:name'destdir'未定义

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

提问于
用户回答回答于

您需要将第10行更改为:

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

在你的情况下,Python 首先在testdir内部查找GetNums,如果在那里找不到它,它将在模块中查找此名称。它不会奇迹般地使用tesdir__init__。该名称self代表您稍后将创建的实例。所以在__init__你基本上设置mysinstance.testdir和稍后GetNums 你可以访问mysinstance.testdirself只是占位符mysinstance,即您稍后创建的实例。

您可以阅读文档中的详细信息

用户回答回答于
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)

扫码关注云+社区

领取腾讯云代金券