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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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工程发现有些包下载不下来