Python处理CSV文件

计算对应空行的和输出到单独的CSV文件 

Python处理CSV文件_第1张图片

结果是这样的(我这里处理了多个上面截图的CSV文件)

Python处理CSV文件_第2张图片

 

下面附代码

import os
import pandas as pd
import numpy as np
import xlwt


workbook = xlwt.Workbook()
sheet = workbook.add_sheet("Sheet Name1")

file_path = "D:\四维\zdh\db1"
path_list = os.listdir(file_path)
path_name=[]
i = 0
# 利用循环历遍path_list列表并且利用split去掉后缀名
for orderid in path_list:
    #读取文件
    try:#文件不存在跳过
        df = pd.read_csv("D:\四维\zdh\db1/"+ orderid +"/RULER_LINE_ABSOLUTE_LANEBOUNDARY_-distinct.csv")

        had = df[["feature_length"]]#had上报总数
        shen = df[["report_length"]]#识别总数
        shen1 = df.query('report_length == "nan"').feature_length.sum()#漏识别
        shen2 = df.query('feature_length == "nan"').report_length.sum()#冗余

        i = i + 1
        #输出数据
        sheet.write(i, 0, str(df.iloc[2]["order_id"]))
        sheet.write(i, 1, float(np.sum(shen)))
        sheet.write(i, 2, float(np.sum(had)))
        sheet.write(i, 3, float(shen1))
        sheet.write(i, 4, float(shen2))

        #创建并输出文件
        #workbook.save("D:\四维\zdh\db0/"+ orderid +'/'+orderid +'.csv')
    except:
        orderid
        #print(orderid+ '该文件不存在')

sheet.write(0, 0, 'orderid')
sheet.write(0, 1, 'FZ总数')
sheet.write(0, 2, 'HD总数')
sheet.write(0, 3, '遗漏')
sheet.write(0, 4, '误新增')

workbook.save("D:\四维\zdh\db1/LANEBOUNDARY.csv")

你可能感兴趣的:(python,开发语言,后端)