向Python类传递参数
我创建了一个Python类来解析文本输入文件,以获得CSV格式的文件。 下面是我的班级代码:向Python类传递参数,python,pyspark,Python,Pyspark,我创建了一个Python类来解析文本输入文件,以获得CSV格式的文件。 下面是我的班级代码: import os from os import listdir class MyClass: def __init__(self, filename, colsList): self.filename=filename self.colsList=colsList def textcsvconverter(self,filename,colsList
import os
from os import listdir
class MyClass:
def __init__(self, filename, colsList):
self.filename=filename
self.colsList=colsList
def textcsvconverter(self,filename,colsList):
import csv
print("colsList:",colsList)
self.cols=colsList
outputfilename=(os.path.basename(filename))
print("outputfilename:",outputfilename)
fname_out =(outputfilename + '.csv')
with open(filename) as fin, open(fname_out, 'wt') as fout:
writer = csv.writer(fout, delimiter=",", lineterminator="\n")
for line in fin:
line = line.rstrip() # removing the '\n' and other trailing whitespaces
data = [line[c[0]:c[1]] for c in cols]
writer.writerow(data)
return fname_out
现在,我已经在Pyspark代码中导入了这个类,并尝试访问class方法,如下所示:
myobjectx = MyClass()
colsListA = [(0,1), (1,23), (23,31), (31,35),(35,41)]
outputfile1=myobjectx.textcsvconverter(finalpath1,colsListA)
它给我以下错误信息:
TypeError: __init__() takes exactly 3 arguments (1 given)
您已经用带有3个参数的init方法声明了类。但是你已经输入了它。正如代码所示,可以在init方法中获得默认值
def __init__(self, filename=None, colsList=[]):
self.filename=filename
self.colsList=colsList
因此,您可以声明您的实例,而无需在其中添加任何参数
myobjectx = MyClass()
而且,您可以像现在使用textcsvconverter
方法一样惰性地分配或放入参数
更新
正如您在下面的评论,我可以看到您正在尝试使用特定输入创建类的实例:
finalpath1 = 'your-filename.csv' # I assume you have it
colsListA = [(0,1), (1,23), (23,31), (31,35),(35,41)]
myobjectx = MyClass(finalpath1,colsListA)
outputfile1=myobjectx.textcsvconverter()
您必须更新您的
textcsvconverter
才能使用self.attribute。您必须使用带有3个参数的init方法声明您的类。但是你已经输入了它。正如代码所示,可以在init方法中获得默认值
def __init__(self, filename=None, colsList=[]):
self.filename=filename
self.colsList=colsList
因此,您可以声明您的实例,而无需在其中添加任何参数
myobjectx = MyClass()
而且,您可以像现在使用textcsvconverter
方法一样惰性地分配或放入参数
更新
正如您在下面的评论,我可以看到您正在尝试使用特定输入创建类的实例:
finalpath1 = 'your-filename.csv' # I assume you have it
colsListA = [(0,1), (1,23), (23,31), (31,35),(35,41)]
myobjectx = MyClass(finalpath1,colsListA)
outputfile1=myobjectx.textcsvconverter()
你必须更新你的
textcsvconverter
才能使用你的self.attribute。据我所知,你在init方法中使用了两个参数。这就是原因,你会得到错误
TypeError: __init__() takes exactly 3 arguments (1 given)
解决方案是,您应该像这样更改init方法
def __init__(self, filename=None, colsList=[]):
self.filename=filename
self.colsList=colsList
或
在上述第二种情况下,您需要修改整个代码
import os
from os import listdir
class MyClass:
def __init__(self, filename, colsList):
self.filename=filename
self.colsList=colsList
def textcsvconverter(self):
import csv
print("colsList:",self.colsList)
self.cols=self.colsList
outputfilename=(os.path.basename(self.filename))
print("outputfilename:",outputfilename)
fname_out =(outputfilename + '.csv')
with open(self.filename) as fin, open(fname_out, 'wt') as fout:
writer = csv.writer(fout, delimiter=",", lineterminator="\n")
for line in fin:
line = line.rstrip() # removing the '\n' and other trailing whitespaces
data = [line[c[0]:c[1]] for c in cols]
writer.writerow(data)
return fname_out
colsListA = [(0,1), (1,23), (23,31), (31,35),(35,41)]
myobjectx = MyClass(finalpath1,colsListA)
outputfile1=myobjectx.textcsvconverter()
据我所知,您在init方法中使用了这两个参数。这就是原因,你会得到错误
TypeError: __init__() takes exactly 3 arguments (1 given)
解决方案是,您应该像这样更改init方法
def __init__(self, filename=None, colsList=[]):
self.filename=filename
self.colsList=colsList
或
在上述第二种情况下,您需要修改整个代码
import os
from os import listdir
class MyClass:
def __init__(self, filename, colsList):
self.filename=filename
self.colsList=colsList
def textcsvconverter(self):
import csv
print("colsList:",self.colsList)
self.cols=self.colsList
outputfilename=(os.path.basename(self.filename))
print("outputfilename:",outputfilename)
fname_out =(outputfilename + '.csv')
with open(self.filename) as fin, open(fname_out, 'wt') as fout:
writer = csv.writer(fout, delimiter=",", lineterminator="\n")
for line in fin:
line = line.rstrip() # removing the '\n' and other trailing whitespaces
data = [line[c[0]:c[1]] for c in cols]
writer.writerow(data)
return fname_out
colsListA = [(0,1), (1,23), (23,31), (31,35),(35,41)]
myobjectx = MyClass(finalpath1,colsListA)
outputfile1=myobjectx.textcsvconverter()
经过一个小改动后,它现在可以工作了:-)cols=self.colsList感谢大家的帮助和支持经过一个小改动后,它现在可以工作了:-)cols=self.colsList感谢大家的帮助和支持文件名和
colsList
都是必须的参数。您应该在实例化对象时提供其值。(即,myobjectx=MyClass(您的文件名,您的colsList))filename
和colsList
都是强制参数。您应该在实例化对象时提供其值。(即,myobjectx=MyClass(您的文件名,您的colsList))谢谢:-)我已经按照建议进行了更改。我现在得到以下错误:数据=[行[c[0]:c[1]]为c in cols]名称错误:未定义全局名称“cols”。。这就是我通过Pyspark colsListA=[(0,1),(1,23),(23,31),(31,35),(35,41)]myobjectx=MyClass(finalpath1,colsListA)outputfile1=myobjectx.textcsvconverter()调用的方式。谢谢:-)我已经按照建议进行了更改。我现在得到以下错误:数据=[行[c[0]:c[1]]为c in cols]名称错误:未定义全局名称“cols”。。这是我通过Pyspark colsListA=[(0,1),(1,23),(23,31),(31,35),(35,41)]myobjectx=MyClass(finalpath1,colsListA)outputfile1=myobjectx.textcsvconverter()调用的方式。这是我现在调用我的类的方式colsListA=[(0,1),(1,23),(23,31),(31,35),(35,41)]myobjectx=MyClass(finalpath1,colsListA)outputfile1=myobjectx.textcsvconverter()我在下面收到错误消息self.colsList=cols NameError:全局名称“cols”未定义这就是我现在调用类的方式colsListA=[(0,1),(1,23),(23,31),(31,35),(35,41)]myobjectx=MyClass(finalpath1,colsListA)outputfile1=myobjectx.textcsvconverter()下面是错误消息self.colsList=cols name错误:未定义全局名称“cols”