首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >未在Python类-Need中定义的OOP变量有助于理解__init__

未在Python类-Need中定义的OOP变量有助于理解__init__
EN

Stack Overflow用户
提问于 2018-08-04 04:24:23
回答 2查看 105关注 0票数 0

我是OOP的新手,我一直在尝试编写一个我可以导入的类,它将帮助我解析文件。我意识到我不需要创建一个类来做到这一点,但我想我应该尝试这样做,这样我就可以开始熟悉OOP了。

这段代码可以工作

代码语言:javascript
复制
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)

这段代码不支持,我也不确定为什么不支持。

代码语言:javascript
复制
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行,在带有open (os.path.join(destdir,GetNums ),'r')的源中,as infile: NameError: name 'destdir‘is not defined

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-04 04:37:25

您需要将第10行更改为:

代码语言:javascript
复制
with open (os.path.join(self.destdir, destination), 'w') as outfile:

在您的示例中,Python首先在GetNums中查找testdir,如果在那里找不到它,它将在模块中查找此名称。它不会神奇地使用__init__中的tesdir。名称self代表稍后创建的实例。因此,在__init__中,您基本上设置了mysinstance.testdir,然后在GetNums中,您可以使用mysinstance.testdir进行访问。self只是mysinstance的占位符,也就是您稍后创建的实例。

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

票数 1
EN

Stack Overflow用户

发布于 2018-08-04 04:47:03

@Mike Müller成功了,但这里是完整的更正代码。

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51679658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档