Python读取csv文件并提取数据到excel
背景:工作使然的缘故需要将存储跑出来大量的fio数据解析整理到excel,并将其中性能低的数据显著的显示出来,于是使用python写了一个数据处理的脚本。
输入:fio解析数据csv
输出:所有盘数据整理到excel横向对比result.xlsx
fio数据格式:
filename,rw,bandwidth(MB/s),iops,avg_latency(usec),max_latency(usec)
sdad_libaio_read_4K_1Job_1Dep_fio_read,read,68.58,16700.0,58.15,21439
sdad_libaio_read_4K_1Job_2Dep_fio_read,read,93.53,22800.0,85.84,29726
sdad_libaio_read_4K_1Job_4Dep_fio_read,read,95.21,23200.0,170.22,36416
sdad_libaio_read_4K_1Job_8Dep_fio_read,read,99.41,24300.0,327.80,35322
sdad_libaio_read_4K_1Job_16Dep_fio_read,read,100.87,24600.0,647.47,78942
sdad_libaio_read_4K_1Job_32Dep_fio_read,read,103.28,25200.0,1267.14,151885
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv #要处理的是csv文件
import sys
import os
import string
import fnmatch
import openpyxl #python3 处理excel 模块
from openpyxl import Workbook
from openpyxl.styles import fills,colors,NamedStyle,Font,Side,Border,PatternFill,Protection,Alignment #excel 单元格格式模块
#sys.path.append(os.path.abspath(__file__)
Cur_path = os.path.abspath('.')
width = 6 #excel cell (x,y)x=0,y=1 means A1
Title = ['Block size','IO type','WR/RD','Jobs','Queue Depth','bandwidth(MB/s)','IOPS']
excel_name = "result.xlsx"
def ex_file(mycsvfile, width):
with open(mycsvfile,"r") as mycsvfile:
if not os.path.exists(excel_name):
workbook= Workbook()
mysheet = workbook.create_sheet(title='sequence',index=0)
workbook.create_sheet(title='random', index=1)
else:
workbook = openpyxl.load_workbook(excel_name)
mysheet = workbook['sequence']
#portion = os.path.splitext(mycsvfile)
csvreader = csv.reader(mycsvfile)
print(csvreader)
row = 3
row2 = 3
count = 3
count2 = 3
for