Python/html-将多个html合并为一个 [英] Python/html- Combine multiple html's into one

查看:268
本文介绍了Python/html-将多个html合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've written a python script to convert a text file to html file. But that is kind of useless if I can't put them all together. What I'm supposed to do is display all the reports onto the website (the server part is not my problem). Now I can convert each file to an html but I just realize it's a huge library of files. How do I combine them all?

这就是我正在考虑如何将它们放在一起的内容,例如:
说这是主页:

Here's what i'm thinking how to put them together, e.g:
Say this is the homepage:

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

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

一些类似的超链接(这里的链接只是伪造的.只是向您展示我的想法)...用户将单击它以查看报告.比到处无处不在的所有html文件更有条理-这正是我在想的大事.
但是问题是,如何在特定日期字段下自动合并所有html报告.
有针对性的指南吗?我完全迷路了,我不知道从哪里开始

Some hyperlinks like that (the links here are just fake. Just showing you what i'm thinking of)...the user will click on it to see the report. Much more organized than all the html files laying around everywhere -- This is just what i'm thinking out loud.
But the problem is how do I automatically have all the html reports combined under a certain date field.
Is there a guide for this? I'm totally lost, I don't know where to start

推荐答案

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

Create a list of tuples in Python. Then sort them in place. Then iterate over the list and produce your homepage HTML. Below an example. You need to fill in the URLs and the date for each report (either as a date object or as a string, example: '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.

以下一些URL可能会有所帮助:

如何在适当的位置对列表进行排序

一般的Python数据结构

这篇关于Python/html-将多个html合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆