Python Class 在调用 class 时出现自定位参数错误
Python Class with Self - Positional argument errors while calling the class
我自己有问题,我写了一个 class 来对我作为 csv 输入的两个数据集进行数据比较 - 在尝试调用此函数时,我遇到了 TypeError .
这是我的 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))
这是错误,我的比较函数应该同时接受源和目标,但是当我调用比较函数时它似乎只接受一个参数。
~/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
你在构造函数中定义了两个参数,所以你必须先像这样调用构造函数
com_files = compare_files("source.csv", "target.csv")
然后像这样调用没有两个参数的compare_csv
方法
all_mistakes, time_taken = com_files.compare_csv()
我自己有问题,我写了一个 class 来对我作为 csv 输入的两个数据集进行数据比较 - 在尝试调用此函数时,我遇到了 TypeError .
这是我的 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))
这是错误,我的比较函数应该同时接受源和目标,但是当我调用比较函数时它似乎只接受一个参数。
~/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
你在构造函数中定义了两个参数,所以你必须先像这样调用构造函数
com_files = compare_files("source.csv", "target.csv")
然后像这样调用没有两个参数的compare_csv
方法
all_mistakes, time_taken = com_files.compare_csv()