废话不多说,估计只有我这个菜鸟废了2个小时才搞出来,主要是我想了太多方法来实现,最后都因为这因为那的原因失败了
间接说明自己对可变与不可变类型的了解,还是不够透彻
最后就用了个笨方法解决了!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#coding:utf-8 import xlrd import xlwt import os from collections import Counter workbook = xlrd.open_workbook(r'D:\hh\heli\a.xlsx') Sheet1 = workbook.sheet_by_name('Sheet1') clos_a=Sheet1.col_values(1) clos_b=Sheet1.col_values(4) clos_c=Sheet1.col_values(5) wbk = xlwt.Workbook() sheet = wbk.add_sheet(u'初始表') sheet.write(0, 0, u'小区') sheet.write(0, 1, u'总端口数') sheet.write(0, 2, u'已使用端口数') test=('D:\hh\heli\\美女你好.xls').decode('utf-8') #表名是不是让你想入非非了,哈哈 result = [] list_m=map(lambda x,y:[x,y],clos_a,clos_b) list_n=map(lambda q,w:[q,w],clos_a,clos_c) dic={} dic_a={} def read_excel(): while test is True: os.remove(test) else: for elem in list_m: key=elem[0] if key in dic: dic[key]+=elem[1] else: dic[key]=elem[1] for elem_a in list_n: key_a = elem_a[0] if key_a in dic_a: dic_a[key_a]+=elem_a[1] else: dic_a[key_a] = elem_a[1] item=list(dic) item_a=list(dic_a) for i in range(1,len(item)): sheet.write(i,0,item[i]) sheet.write(i,1,dic[item[i]]) for p in range(1,len(item_a)): sheet.write(p,2,dic_a[item_a[p]]) wbk.save(test) if __name__ == "__main__": read_excel() |
以上这篇python对excel文档去重及求和的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/louishu_hu/article/details/54612435