zoukankan      html  css  js  c++  java
  • python之小应用:读取csv文件并处理01数据串

    目的:读取csv文件内容,把0和1的数据串取出来,统计出现1的连续次数和各次数出现的频率次数

    先读取csv文件内容:

    
    
    import csv
    def csv_read(file):
        list = []
        csv_reader = csv.reader(file)
        for id, data, *args in csv_reader:
            #跳过表头
            if id == "   ":
                continue
            #print(id, data)
            list.append(data)
        return list
    
    


    再写处理0和1的方法

    
    
    #统计连续0和1出现的个数
    
    #函数功能:对连续出现的1的个数进行统计,返回一个连续次数列表
    def sum_times(list):
        total_list = []
        #n统计出现次数,m表示当前处理个数
        n = 0
        m = 0
        for w in list:
            m += 1
            if int(w) == 1:
                n += 1
            elif int(w) == 0:
                if n > 0:
                    #连续次数大于2则打印位置
                    if n > 2:
                        print("连续时长:", n,"| 行数:", m-n)
                    total_list.append(n)
                    n = 0
            #如果最后一个为1则自动计数
            if m == len(list):
                if n > 0:
                    total_list.append(n)
                    n = 0
    
        print("
    sum_times函数打印连续次数列表:
    ", total_list)
        return total_list
    
    #函数功能:对出现频率列表进行统计
    def sum_tocal(list):
        list_total = [0, 0, 0, 0, 0]
        for n in list:
            if n == 1:
                list_total[0] += 1
            elif n == 2:
                list_total[1] += 1
            elif n == 3 or n == 4:
                list_total[2] += 1
            elif n == 5 or n == 6:
                list_total[3] += 1
            elif 6 < n <= 12:
                list_total[4] += 1
        return list_total
    
    


    最后依次调用执行

    
    
    import readcsv, tong_ji_ge_shu
    #统计出现各时长频率的次数
    
    file = open("d://vis_911_3000.csv")
    #file = open("d://ts_ctime_12.csv")
    
    #从csv中读取数据
    csvlist = readcsv.csv_read(file)
    file.close()
    
    #记录连续出现的小时数
    list_pinlv = tong_ji_ge_shu.sum_times(csvlist)
    
    #统计各时长出现的个数
    list_final = tong_ji_ge_shu.sum_tocal(list_pinlv)
    
    print() #空行
    print(list_final) #输入各时长个数的统计列表
    #print("1次:" + str(list_final[0]), " |  2次:" + str(list_final[1]), " |  3-4次:" + str(list_final[2]))
    print("[1次,2次,3-4次,5-6次,7-12次]")
    
    
    


  • 相关阅读:
    mysql 库,表,数据操作
    mysql 初识数据库
    MySQL 索引 视图 触发器 存储过程 函数
    MySQL 事物和数据库锁
    MySQL 约束和数据库设计
    MySQL 创建千万集数据
    MySQL 各种引擎
    求1,1,2,3,5,8,13 斐波那契数列第N个数的值
    WEB前端研发工程师编程能力成长之路(1)
    XML DOM
  • 原文地址:https://www.cnblogs.com/gongxr/p/7223683.html
Copyright © 2011-2022 走看看