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次]")
    
    
    


  • 相关阅读:
    -bash: fork: Cannot allocate memory 问题的处理
    Docker top 命令
    docker常见问题修复方法
    The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
    What's the difference between encoding and charset?
    hexcode of é î Latin-1 Supplement
    炉石Advanced rulebook
    炉石bug反馈
    Sidecar pattern
    SQL JOIN
  • 原文地址:https://www.cnblogs.com/gongxr/p/7223683.html
Copyright © 2011-2022 走看看