簡體   English   中英

Python類中未定義的OOP變量-需要幫助了解__init__

[英]OOP Variable Not Defined in Python Class -Need Help Understanding __init__

我是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中,以infile格式打開(os.path.join(destdir,source),'r'):NameError:未定義名稱'destdir'

據我了解,類對象的名稱空間將允許類中的函數查看該類中聲明的所有變量。

您需要將第10行更改為:

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的占位符,即您以后創建的實例。

您可以在文檔中閱讀詳細信息

@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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM