我是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行,在带有open (os.path.join(destdir,GetNums ),'r')的源中,as infile: NameError: name 'destdir‘is not defined
我的理解是,类对象的命名空间将允许类中的函数看到该类中声明的所有变量。
发布于 2018-08-04 04:37:25
您需要将第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
的占位符,也就是您稍后创建的实例。
您可以在documentation中阅读详细信息。
发布于 2018-08-04 04:47:03
@Mike Mü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)
https://stackoverflow.com/questions/51679658
复制相似问题