from itertools import groupby
with open(r"codeMaster.csv") as fp:
line = fp.readlines()
line = [i.strip() for i in line]
print([list(g) for k, g in groupby(line, key=bool) if k])
with open(r"CodeMaster.csv") as fp:
line = fp.readlines()
line = [i.strip() for i in line]
result = [[]]
for i in line:
if not i:
result.append([])
else:
result[-1].append(i)
print(result)
import csv
final_list = []
with open('filename.csv') as file:
reader = csv.reader(file)
temp_list = []
for row in reader:
if not row:
final_list.append(temp_list)
temp_list = []
else:
temp_list.append(row)
final_list.append(temp_list)
print(final_list)
2条答案
按热度按时间qoefvg9y1#
只要一行一行地读取csv文件&列表中的多个空字符串,就可以像这样使用itertools.groupby:
给出编号
更多的Python##
还提供#
dz6r00yl2#
若要读取.csv文件并将其分成由空格分隔的组,可以使用以下代码: