首页 > 解决方案 > 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")

但它不起作用。在这里需要你的帮助。

标签: python

解决方案


好的,这是代码。我硬编码了一个值,但这足以开始。

 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

注意请务必更改日志的文件位置

在此处输入图像描述


推荐阅读