新手python刚刚上路,在实际工作中遇到如题所示的问题,尝试使用python3简单实现如下,欢迎高手前来优化

import
csv #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() with open("dk0519_1.csv","r",encoding="utf-8") as csv_file: #读取csv文件,返回的是迭代类型 read = csv.reader(csv_file) alist = [] bdict = [] cdict = [] tmp = [] for i in read: alist.append(i[0].split("\t")) for a in alist[1:]: print(a) bdict.append((a[0].split("?")[0],a[1])) for b in bdict: num = int(b[1]) j = bdict.index(b) + 1 while j <= (len(bdict)-1) : if b[0] not in tmp: if b[0] == bdict[j][0]: num += int(bdict[j][1]) j += 1 else: j +=1 else: j +=1 if b[0] not in tmp: cdict.append((b[0],num)) else: pass tmp.append(b[0]) with open('re_dk0519_1.csv','w',encoding="utf-8") as write_csvfile: writer = csv.writer(write_csvfile) writer.writerows(cdict)

 

相关文章:

  • 2021-10-03
  • 2021-10-08
  • 2022-02-09
  • 2022-03-07
  • 2021-12-14
  • 2021-07-12
  • 2021-11-01
  • 2022-12-23
猜你喜欢
  • 2021-11-23
  • 2021-07-22
  • 2021-05-29
  • 2023-02-08
  • 2022-12-23
  • 2022-12-23
  • 2021-09-05
相关资源
相似解决方案