我有一个作业问题,我们必须按降序按第3列对特定的csv文件进行排序。然后,我们必须返回第3列中所有具有最大值的行,并且由于我们尚未学习如何使用它们而不能使用熊猫。我们必须对它具有功能的代码进行编码,以便教授可以将我们的函数调用到他的代码中并看到它的执行结果。
def bigRow():
new_row = []
with open('assignment2Data.csv', 'rU', newline='') as f:
reader = csv.reader(f, delimiter='^')
data = [x for x in reader]
max_thirdcol_val = max([x[2] for x in data])
for row in data:
if row[2] == max_thirdcol_val:
new_row.append(row)
return new_row
我提交它时出错,原因是:
File "UNITTEST.py", line 21, in test_unit
assert(bigRow('assignment2Data.csv', 3)==answer or bigRow('assignment2Data.csv', 3)==answer2)
TypeError: bigRow() takes 0 positional arguments but 2 were given
答案 0 :(得分:0)
测试代码试图使用两个参数(文件名和列索引)调用函数。因此,您需要将函数编写为:
def bigRow(filename, column_index):
with open(filename, 'rU', newline='') as f:
reader = csv.reader(f, delimiter='^')
data = [x for x in reader]
# TODO: Complete this function :)