请帮我用OpenPyxl解析文件。
我有一个excel文件如下:
输入xlsx:
A B C
1 Tom Red True
2 Tom Red False
3 Marry Green False
4 Marry Green True
5 Babara Red False
A + B可能是删除重复值的关键。 (例如Marry + Green,Tom + Red) 读取输入文件并生成如下输出文件:
预期输出xlsx:
A B Result
1 Tom Red True,False
2 Marry Green False,True
3 Babara Red False
**值"真,假"在第1排。
答案 0 :(得分:1)
尝试:
import collections
from openpyxl import load_workbook
wb1 = load_workbook('test.xlsx')
ws1 = wb1['test']
a_dict = collections.defaultdict(list)
for row in ws1.rows:
a_dict[row[0].value+','+row[1].value].append(str(row[2].value))
wb2 = Workbook(write_only=True)
ws2 = wb2.create_sheet()
for key,value in a_dict.items():
temp = key.split(',')
temp.append(','.join(value))
ws2.append(temp)
wb2.save('new_test.xlsx')
new_test.xlsx
将是: