Python/html- 将多个 html 合并为一个

BPm*_*BPm 1 html python

我写了一个 python 脚本来将文本文件转换为 html 文件。但如果我不能把它们全部放在一起,那就没什么用了。我应该做的是将所有报告显示到网站上(服务器部分不是我的问题)。现在我可以将每个文件转换为 html,但我刚刚意识到这是一个巨大的文件库。我如何将它们全部结合起来?

这就是我在想如何将它们组合在一起,例如:
假设这是主页:

日期
-报告 1
-报告 2
-报告 3
...

像这样的一些超链接(这里的链接只是假的。只是向您展示我在想什么)...用户将单击它来查看报告。比随处可见的所有 html 文件更有组织性——这正是我大声思考的。
但问题是我如何自动将所有 html 报告合并到某个日期字段下。
有这方面的指南吗?我完全迷失了,我不知道从哪里开始

Mic*_*ael 6

在 Python 中创建元组列表。然后将它们分类到位。然后遍历列表并生成主页 HTML。下面是一个例子。您需要填写每个报告的 URL 和日期(作为日期对象或字符串,例如:'09-12-2011')

report_tuples = [
    ('http://www.myreport.com/report1', report1_date_object_or_string),
    ('http://www.myreport.com/report2', report2_date_object_or_string),
    ('http://www.myreport.com/report3', report3_date_object_or_string),
]
sorted(report_tuples, key=lambda reports: reports[1])   # sort by date
html = '<html><body>' #add anything else in here or even better 
                      #use a template that you read and complement
lastDate = None
for r in report_tuples:
    if not lastDate or not lastDate == r[1]:
        html += '<h3>%s</h3>' % (str(r[1]))
    html += '<a href="%s">Your Report Title</a>' % (r[0])

return html #or even better, write it to the disk.
Run Code Online (Sandbox Code Playgroud)

这里有一些可能有帮助的网址:

如何就地排序列表

Python 数据结构概述