答案 0 :(得分:1)
解决此类问题的最简单方法是首先将电子表格导出为CSV,以帮助确保将其正确导入到Python中。
使用defaultdict,您可以创建一个具有唯一键的字典,然后遍历将最终列的值添加到列表中的行。
最后,您可以将其写回CSV格式:
from collections import defaultdict
results = defaultdict(list)
with open("in_file.csv") as f:
header = f.readline()
for line in f:
cols = line.split(",")
key = ",".join(cols[0:4])
results[key].append(cols[4])
with open("out_file.csv", "w") as f:
f.write(header)
for k, v in results.iteritems():
line = '{},"{}",\n'.format(k, ", ".join(v))
f.write(line)