I am having issues with self, I have written a class to make a data comparison of two datasets which I am feeding in as a csv - while trying to call this function, I am getting a TypeError.
Here is my class
import csv
import time
import pandas as pd
start_time = time.time()
class compare_files:
def __init__(self, source, target):
self.source = source
self.target = target
def compare_csv(self):
source_file = open(self.source, "r", encoding="utf8")
target_file = open(self.target, "r", encoding="utf8")
csv_reader_source = csv.reader(source_file, delimiter=',')
csv_reader_target = list(csv.reader(target_file, delimiter=','))
columns = []
all_mistakes = []
target_line = 0
for row_source in csv_reader_source:
if target_line == 0:
for i in row_source:
columns.append(i)
target_line = 1
continue
row_target = csv_reader_target[target_line]
target_line += 1
mistakes = []
if len(row_target) != len(row_target):
if len(mistakes) == 0:
mistakes.append(row_source[0])
mistakes.append('number of columns in this row mismatch')
for i in range(len(row_source)):
if row_source[i] != row_target[i]:
if len(mistakes) == 0:
mistakes.append(row_source[0])
mistakes.append(columns[i])
mistakes.append(row_source[i])
mistakes.append(row_target[i])
if len(mistakes) > 0:
all_mistakes.append(mistakes)
return all_mistakes, round(time.time() - start_time, 2)
all_mistakes, time_taken = compare_files.compare_csv(
"source.csv", "target.csv")
for i in all_mistakes:
print(i)
print("Execution time took %s seconds" % (time_taken))
and here is the error, My compare function should accept source and target both, but when I calling compare function it seems it only accepts one argument.
~/Desktop » python3 compar2.py
Traceback (most recent call last):
File "compar2.py", line 70, in <module>
all_mistakes, time_taken = compare_files.compare_csv(
TypeError: compare_csv() takes 1 positional argument but 2 were given
you defined the two parameters in the constructor so you have to call the constructor first like this
com_files = compare_files("source.csv", "target.csv")
then call the compare_csv
method without the two parameters like this
all_mistakes, time_taken = com_files.compare_csv()