【问题标题】:Python/html- Combine multiple html's into one [closed]Python / html-将多个html合并为一个[关闭]
【发布时间】:2011-09-27 16:59:05
【问题描述】:

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

这是我正在考虑如何将它们组合在一起的方法,例如:
说这是主页:

Date:
- Report 1
- Report 2
- Report 3
...

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

【问题讨论】:

  • 确实了解这些链接指向您本地计算机上的文件,因此对我们其他人来说毫无价值,对吧?
  • 我知道。但这只是一个例子
  • 什么的例子?没有亲自坐在您的计算机旁的任何人都无法看到您提供的链接中的 html
  • 对于链接,您可以只使用相对 url。如果您不显示某些数据,我们无法告诉您如何拆分报告。
  • 我们怎么知道这个日期?

标签: python html


【解决方案1】:

在 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.

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

How to sort a list in place

Python data structures in general

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2011-06-14
    • 2017-11-22
    • 1970-01-01
    • 2015-05-19
    • 2018-04-09
    • 1970-01-01
    相关资源
    最近更新 更多