提取日志文件数据并直接输入到xhtml主体中

问题描述 投票:0回答:1

我目前有一个python脚本,其中放置了一个日志文件,所有已定义的'excluded'关键字都剥离在同一文件中。我试图在提取所需的单词之后,将其输入到预先构建的XHTML文件中,直接输入到“正文”部分。

有没有办法做到这一点?

我用于将提取的日志文件写入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>

谢谢。

python html xhtml
1个回答
0
投票

这怎么样?

# 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)
© www.soinside.com 2019 - 2024. All rights reserved.