提取日志文件数据并直接输入xhtml正文

2024-05-12 17:15:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在有一个python脚本,其中有一个日志文件,所有定义的“排除”关键字都在同一个文件中剥离。然后,在提取了所需的单词之后,我尝试将其直接输入到一个预构建的XHTML文件的“body”部分中

有没有办法做到这一点

我将从提取的日志文件写入XHTML文件的代码如下,但这将覆盖当前的XHTML文件(我希望这是我遇到的问题)

我已经阅读了BeautifulSoup,但我不想走这条路,我希望严格地将所有这些都保存在python文件中执行(如果可能的话)

contents = open('\path\to\file.log','r')
with open("output.html", "w") as writehtml:
    for lines in contents.readlines():
        writehtml.write("<pre>" + lines + "</pre> <br>\n")

本节中我的XHTML页面的格式如下:

                <body>
                <tr>            
                    <td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px;">
                        <table border="1" cellpadding="0" cellspacing="0" width="100%%">
                            <tr>
                                <td style="padding: 10px 0 10px 0; font-family: Calibri, sans-serif; font-size: 16px;">
                                    <!-- Body text from file goes here-->
                                    Body Text Replaces Here
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
                        </table>
                    </td>
                </tr>
                </body>

谢谢


Tags: 文件stylecontentstablebodyopenpretr
1条回答
网友
1楼 · 发布于 2024-05-12 17:15:30

这是怎么回事

# You can read the template data and spell it in
contents = open('\path\to\file.log','r')
# Suppose that the beginning of your template is stored in this file,\path\template\start.txt
start = '''
<body>
            <tr>            
                <td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px;">
                    <table border="1" cellpadding="0" cellspacing="0" width="100%%">
                        <tr>
                            <td style="padding: 10px 0 10px 0; font-family: Calibri, sans-serif; font-size: 16px;">
'''
# start = open('\path\template\start.txt','r')
# Assume that the end of your template is in this file,\path\template\end.txt
end = '''
</td>
                        </tr>
                    </table>
                </td>
            </tr>
                    </table>
                </td>
            </tr>
            </body>
'''
# end = open('\path\template\end.txt','r')
with open("output.html", "a") as writehtml:
    writehtml.write(start)
    for lines in contents.readlines():
        writehtml.write("<pre>" + lines + "</pre> <br>\n")
    writehtml.write(end)

相关问题 更多 >