当前位置:主页 > 软件编程 > Python代码 >

python对excel文档去重及求和的实例

时间:2020-10-05 21:33:36 | 栏目:Python代码 | 点击:

废话不多说,估计只有我这个菜鸟废了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()

您可能感兴趣的文章:

相关文章