python对excel文档去重及求和的实例
作者:louishu_hu 时间:2021-09-30 03:30:35
废话不多说,估计只有我这个菜鸟废了2个小时才搞出来,主要是我想了太多方法来实现,最后都因为这因为那的原因失败了
间接说明自己对可变与不可变类型的了解,还是不够透彻
最后就用了个笨方法解决了!
#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()
来源:https://blog.csdn.net/louishu_hu/article/details/54612435
标签:python,excel,求和,去重
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中通过selenium简单操作及元素定位知识点总结
2021-07-18 15:34:37
![](https://img.aspxhome.com/file/2023/5/95285_0s.png)
windows下Python实现将pdf文件转化为png格式图片的方法
2022-03-22 08:55:51
动态规划之矩阵连乘问题Python实现方法
2022-04-08 02:07:38
![](https://img.aspxhome.com/file/2023/2/130952_0s.jpg)
使用python 爬虫抓站的一些技巧总结
2021-01-02 07:35:57
![](https://img.aspxhome.com/file/2023/2/117352_0s.jpg)
基于javascript如何传递特殊字符
2023-09-06 04:14:23
python3爬虫学习之数据存储txt的案例详解
2022-01-17 05:02:09
![](https://img.aspxhome.com/file/2023/6/113176_0s.png)
Python中根据时间自动创建文件夹的代码实现
2023-07-06 02:42:01
![](https://img.aspxhome.com/file/2023/1/95891_0s.png)
javascript new fun的执行过程
2010-08-05 21:23:00
![](https://img.aspxhome.com/file/UploadPic/20108/5/20100805150736846-93s.jpg)
Z-Blog垃圾留言判定新方法
2009-07-06 13:04:00
CSS关于Border你可能会不注意的东西
2007-10-20 13:50:00
![](https://img.aspxhome.com/file/UploadPic/200710/20/20071020135623390s.gif)
Python生成随机验证码代码实例解析
2023-08-28 20:48:58
django框架中间件原理与用法详解
2022-12-15 03:25:37
![](https://img.aspxhome.com/file/2023/8/83938_0s.png)
Python虽然很火找工作为什么这么难
2022-10-29 11:33:03
![](https://img.aspxhome.com/file/2023/2/132352_0s.jpg)
pycharm配置python 设置pip安装源为豆瓣源
2022-07-16 12:09:43
![](https://img.aspxhome.com/file/2023/0/86250_0s.png)
如何实现Django Rest framework版本控制
2022-10-30 02:29:19
![](https://img.aspxhome.com/file/2023/8/89288_0s.png)
Python面向对象程序设计示例小结
2023-08-24 13:31:24
Python表示矩阵的方法分析
2022-06-24 01:31:59
![](https://img.aspxhome.com/file/2023/2/71342_0s.png)
Cpython解释器中的GIL全局解释器锁
2021-08-29 22:06:43
![](https://img.aspxhome.com/file/2023/7/63907_0s.png)
python爬虫要用到的库总结
2021-04-16 23:55:40
利用Python如何实现一个小说网站雏形
2023-09-22 05:52:07