随笔 - 321  文章 - 0  评论 - 6  阅读 - 34万

esrally结果csv格式报告转html脚本

关于转html的需求:可以先把源文件上传到在线转html的网站,获取一下专业的html格式,然后根据这个html的规律去解析源文件生成html文本

 

#-*-coding:utf-8-*-
#!/bin/pyton
import csv
import pandas as pd
import requests
import json
import datetime

upload_path='report_http_logs_es6.csv'
pd.set_option('display.max_colwidth', -1)
df = pd.read_csv(upload_path, sep='|',usecols=[1,2,3,4],skipinitialspace=True)

html_to_str = df.to_html(index=False,justify='center',col_space=50)
#print(html_to_str)

 

参数说明:

pd.set_option('display.max_colwidth', -1):关闭最大列宽,防止截断

sep:分隔符

usecols:想要读取的列

skipinitialspace:忽略分隔符后的空格

col_space=50:最小列宽,但是好像没生效,没找到原因

justify='center':列标题居中

index=False:关闭表格第一列的数字行号

 

待转换csv格式:

 

 

转换后的html

 

如果还需要做些css相关的优化,可以在生成html之后通过内联的方式来定义css,如下是个简单示例

  html_string = '''
<html>
<head>
<title>HTML Pandas Dataframe with CSS</title>
<style type="text/css">
table.dataframe td {
text-align: right;
}
table.dataframe td[class="high_light"] {
color: red;
}
</style>
</head>
<body>
<p>点击链接查看近一个月的 elasticsearch 集群性能数据:</p>
''' + es_link_month_latency + es_link_month_tps_reader + es_link_month_tps_writer + es_link_month_error_rate + "<p>下表为今日凌晨时分 elasticsearch 集群性能数据</p>" + html_to_str + "</body></html>"

esrally报告指标140多项,如果需要高亮表格文字的话,由于pandas的to_html无法指定td的类型或者id,所以一种方法是通过替换字符串来改写html
如下:
#html高亮部分
html_string = re.sub(r'<td>Max Throughput</td>', r'<td class="high_light">Max Throughput</td>', html_string)
html_string = re.sub(r'<td>90th percentile latency</td>', r'<td class="high_light">90th percentile latency</td>', html_string)
html_string = re.sub(r'<td>error rate</td>', r'<td class="high_light">error rate</td>', html_string)
html_string = re.sub(r'<td>Cumulative indexing time of primary shards</td>', r'<td class="high_light">Cumulative indexing time of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative merge time of primary shards</td>', r'<td class="high_light">Cumulative merge time of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative merge count of primary shards</td>', r'<td class="high_light">Cumulative merge count of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative refresh time of primary shards</td>', r'<td class="high_light">Cumulative refresh time of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative refresh count of primary shards</td>', r'<td class="high_light">Cumulative refresh count of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative flush time of primary shards</td>', r'<td class="high_light">Cumulative flush time of primary shards</td>', html_string)
html_string = re.sub(r'<td>Cumulative flush count of primary shards</td>', r'<td class="high_light">Cumulative flush count of primary shards</td>', html_string)
html_string = re.sub(r'<td>Total Young Gen GC</td>', r'<td class="high_light">Total Young Gen GC</td>', html_string)
html_string = re.sub(r'<td>Total Old Gen GC</td>', r'<td class="high_light">Total Old Gen GC</td>', html_string)

最终效果:

 

 

 

 

参考:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

posted on   该用户很懒  阅读(226)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2019-12-04 jenkins打包maven工程发现有些包下载不下来
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示