我有一个文本文件a.txt。我想对它执行一些预处理,例如remove punct。并将其拆分为单词。
我编写了以下代码以执行一些操作。
class pre:
def __init__(self,textfilepath):
self.textfilepath = textfilepath
def __str__(self,textfilepath):
return str(textfilepath)
def process(textpathfile):
with open(textpathfile, r) as abc:
a = abc.translate(string.maketrans("",""), string.punctuation)
a = a.split(' ')
return a
pre("a.txt")
我尝试执行它。但是它给出了一个错误,即不带参数。有人可以帮我吗?谢谢大家。
您不应该将参数传递给__str__
。相反,您可以通过self
的属性访问它们:
class pre:
def __init__(self,textfilepath):
self.textfilepath = textfilepath
def __str__(self):
return self.textfilepath
def process(self):
with open(self.textfilepath, r) as abc:
a = abc.translate(string.maketrans("",""), string.punctuation)
a = a.split(' ')
return a
p = pre("a.txt")
print(p)
filedata = p.process()
print(filedata)