python - python将日志文件转换为html格式
问题描述
我是python编程部分的新手。有人在这里帮助我会很棒。
我正在使用 python send as html email 将下面的(test1.log)日志输出转换为(test1.html)html。
我已经创建了 python 代码来发送电子邮件。现在我需要将日志转换为 html 格式并将该 html 文件添加到 python_send_email 脚本中。现在我需要帮助,如何将此 test1.log 输出转换为 test.html。
test1.log :
--------------
Process: Failed: threshold limit is high
Memory : Failed: Exceed threshold limit
Disk usage: Failed: Disk size exceeded than 75% size
Swap Memory - host1-sos1 :Passed
Swap Memory -host2-os2 :Passed
Swap Memory -host3-os3 :Passed
****************************
Detailed error
***************************
---------- Disk usage ------------------
NAME Disk Total(GB) Disk usage(%)
---------- -------------------- ----------------
host1-os1 758 89%
Email should send html format as below:
---------------------------------------
----------------------------------------------------------------
| Task name | Status | Failed Reason |
-----------------------------------------------------------------
| Process | Failed | threshold limit is high|
-------------------------------------------------------------------
| Memory | Failed | Exceed threshold limit |
-------------------------------------------------------------------
| Disk Usage | Failed | Disk size exceeded than 75% size|
---------------------------------------------------------------------------
| Swap Memory - host1-sos1 | Passed | |
----------------------------------------------------------------------------
| Swap Memory - host2-sos2 | Passed | |
----------------------------------------------------------------------------
| Swap Memory - host3-sos3 | Passed | |
----------------------------------------------------------------------------
***************************
Detailed error
***************************
---------- Diskusage --------------
NAME OPEN_MODE DATABASE_ROLE
---------- -------------------- ----------------
host1-os1 READ ONLY WITH APPLY PHYSICAL STANDBY
我尝试使用以下代码将日志转换为 html 格式:
代码:
contents = open("test1.log","r")
with open("test1.html", "w") as e:
e.write("<table>")
for lines in contents.readlines():
a,b,c = [ str(x) for x in lines.split(':')]
if '*****' in lines:
break
else:
e.write("<tr><tb>%s</tr><tr><tb>%s</tr></tb><tr><tb>%s</tr></tb>\n"%(a,b,c))
e.write("</table>\n")
但它不起作用。在这里需要你的帮助。
解决方案
好的,这是代码。我硬编码了一个值,但这足以开始。
contents = open("./sample_data/test1.log","r")
with open("test1.html", "w") as e:
e.write("<table>")
for lines in contents.readlines():
print(len(lines.split(':')))
if '*****' in lines:
break
if(len(lines.split(':')) == 3):
a, b, c = [str(x) for x in lines.split(':')]
e.write("<tr><tb>%s</tr><tr><tb>%s</tr></tb><tr><tb>%s</tr></tb>\n"%(a,b,c))
e.write("</table>\n")
e.close()
如果您想在集成之前进行修改,这里是 colab 链接。
https://colab.research.google.com/drive/1ZNjA7UBx7BD6Yi_A5TDcxwgeod_Y6Eoo?usp=sharing
注意请务必更改日志的文件位置
推荐阅读
- scala - 运行测试时模拟匹配器出现问题
- jmeter - Jmeter-如何将大文件从 S3 存储桶复制到同一个 s3 中的另一个存储桶
- sql - 检查数组的交集
- reactjs - 只允许某个域访问我的 Django 后端
- javascript - 如何在html中的单词和字母之间留出空格?
- javascript - 如何根据选择打开嵌套的选项卡行?
- python - python “unicodescape 错误”当我尝试用字符串添加数字时
- javascript - 将 Firebase 存储中的文件保存为 .pdf?
- python - 逐行打印熊猫列名和单元格值
- r - 按 id 折叠行,为 df 中的每个 id 提供 1 行