我正在理解,我不知道如何运行N次for循环。在这种情况下,我想导入.csv文件的前5行。但我想了解进一步解决方案的逻辑。
def columnMatch(self):
with open(self.file_path) as csvfile:
readcsv = csv.reader(csvfile, delimiter=';')
line_count = 0
row_list = []
for row in readcsv:
if line_count < 5:
row_list.append(row)
line_count += 1
return row_list
答案 0 :(得分:-1)
您可以使用enumerate
函数来简化操作,例如:-
...
readcsv = csv.reader(csvfile, delimiter=';')
row_list = []
for i, row in enumerate(readcsv):
if i < 5:
row_list.append(row)
else:
break
...