0

I have created one Python class to parse the text Input File in order to have a file in CSV format. Below is my class code:

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

Now I have imported this class in my Pyspark code and trying to access the class method as shown below:

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

Its giving me below error message:

TypeError: __init__() takes exactly 3 arguments (1 given)
1
  • 1
    Both filename and colsList are mandatory arguments. You should provide its values while instantiating the object . (ie, myobjectx = MyClass(your_filename, you_colsList) ) Commented Oct 16, 2018 at 6:00

3 Answers 3

1

You have declare your class with init method with 3 arguments. but you have input it. As you code show, you can get a default value in the init method.

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

So, that you can declare your instance without put any argument into it.

myobjectx = MyClass()

And, you can lazy assign or put argument in like you are doing with textcsvconverter method now.

UPDATE

As your comment below, I can see you are trying to make an instance of your class with a certain input :

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

And you have to update your textcsvconverter to use your self.attribute.

Sign up to request clarification or add additional context in comments.

1 Comment

This is how I am calling my class now colsListA = [(0,1), (1,23), (23,31), (31,35),(35,41)] myobjectx = MyClass(finalpath1,colsListA) outputfile1=myobjectx.textcsvconverter() I am getting below error message self.colsList=cols NameError: global name 'cols' is not defined
1

As per my knowledge, you took the two arguments in init method. that's the reason, you will get the error.

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

The solution is, you should change your init method something like this.

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

OR

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

In the above second case, you need to modify your whole code.

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

1 Comment

Thanks :-) I have made the changes as suggested. I am getting below error now: data = [line[c[0]:c[1]] for c in cols] NameError: global name 'cols' is not defined.. This is how I am calling through Pyspark colsListA = [(0,1), (1,23), (23,31), (31,35),(35,41)] myobjectx = MyClass(finalpath1,colsListA) outputfile1=myobjectx.textcsvconverter()
0

Its working now after a small change :-) cols=self.colsList Thanks all for your help and support

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.