首页
学习
活动
专区
工具
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
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51679658

复制
相关文章

相似问题

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