繁体   English   中英

将参数传递给Python类

[英]passing arguments to Python Class

我创建了一个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 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代码中导入了该类,并尝试访问该类方法,如下所示:

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。

据我所知,您在init方法中使用了两个参数。 这就是原因,您将得到错误。

 TypeError: __init__() takes exactly 3 arguments (1 given)

解决方案是,您应该像这样更改init方法。

def __init__(self, filename=None, colsList=[]):
    self.filename=filename
    self.colsList=colsList

要么

colsListA = [(0,1), (1,23), (23,31), (31,35),(35,41)]
myobjectx = MyClass(finalpath1, colsListA)
outputfile1=myobjectx.textcsvconverter(finalpath1,colsListA)

在上述第二种情况下,您需要修改整个代码。

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谢谢大家的帮助和支持

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM