zoukankan      html  css  js  c++  java
  • python3 简单实现从csv文件中读取内容,并对内容进行分类统计

    新手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(" ")) 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)
  • 相关阅读:
    厕所惊魂
    感谢协助学生返校,邀请交警合影留念
    小丑杀人
    东芝Toshiba e-STUDIO打印身份证
    JavaScript
    html
    pymysql模块
    线程
    队列与进程池
    网络编程
  • 原文地址:https://www.cnblogs.com/wx2017/p/6921755.html
Copyright © 2011-2022 走看看