Python file.write 在 html 标签内

标签 python html

我刚开始使用 Python,我的第一个脚本是一个非常简单的工作日志。 我运行它,当我完成工作时,我按回车键,它将工作时间放入我与雇用我的人共享的 html 文件中。

这是代码,请不要捂脸太多,这是我的第一步:

#!/usr/bin/python
import datetime
start = datetime.datetime.now()
startt = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print "Counting worktime started."
confirm = raw_input("Press enter when work finished ")
finish = datetime.datetime.now()
delta = finish - start
print datetime.datetime.now()
print delta.seconds
work_time=str(datetime.timedelta(seconds=delta.seconds))
finisht=str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
note = raw_input("What were you doing? ")
line1=['<span>','Work started: <b>', startt, '</b></span><br />']
line1f=' '.join(line1)
line2=['<span>','Work ended: <b>', finisht, '</b></span><br />']
line2f=' '.join(line2)
line3=['<span>','Work duration: <b>', work_time,'</b></span><br />']
line3f=' '.join(line3)
line4=['<span>','Note: <b>', note,'</b></span><br /><br />']
line4f=' '.join(line4)
with open("/srv/www/worklog.html","a") as worklog:
    worklog.write(line1f)
    worklog.write("\n")
    worklog.write(line2f)
    worklog.write("\n")
    worklog.write(line3f)
    worklog.write("\n")
    worklog.write(line4f)
    worklog.write("\n")
    worklog.write("<span> ========================= </span><br /><br />")
    worklog.write("\n")

这是 worklog.html 文件:

<html>
<head></head>
<body style="background-color: #195B83;color:#FFF;margin: 0px;">
    <style>
        span {font-size: 12px;margin-left: 5px;}
        .logo {float: right;margin-right: 10px;margin-top:5px;}
        .first-div {background-color: #FFF; color:#195B83;width:100%;}
        .sec-div {margin-left: 5px;}
    </style>
    <div class="first-div">
        <img src="logo.png" class="logo" />
        <div class="sec-div">
            <h1>simple worklog 1.0</h2>
        </div>
    </div>
    <br />
    <span> ========================= </span><br /><br />
    <span> Work started: <b> 2014-09-11 13:40:26 </b></span> <br />
    <span> Work ended: <b> 2014-09-11 13:40:29 </b></span> <br />
    <span> Work duration: <b> 0:00:02 </b></span> <br />
    <span> Note: <b> Testing </b></span><br /><br />
    <span> ========================= </span><br /><br />

而且有效!

我的问题是——我该如何包含

</body></html>

标签?我尝试使用 .replace,但我的实验失败了,整个文件都被清除了。您能否提示我如何制作此脚本以将它们保留在 worklog.html 的末尾?

编辑:

多亏了下面很棒的提示,我重写了代码,我认为现在它更有意义了,你可以在这里找到它:

main_script(将日志添加到 csv 并将数据添加到网站):http://pastebin.com/ZbCqJ9p9

page_refresher(不添加工作日志,只是将数据放在网站上):http://pastebin.com/3hi077RK

模板(带有 bootstrap css):http://pastebin.com/xZ7VmE1U

数据文件格式:http://pastebin.com/0KNAXuqh

它看起来像这样:http://elysium.c-call.eu/sworklog/

它肯定不是最高等级并且有一些问题,但它比我带来的废话好多了:)

非常感谢。

最佳答案

我认为更清洁的解决方案是稍微改变您的流程。

与其直接记录到 HTML 文件,不如考虑将数据(开始时间和结束时间)存储到 CSV 文件(或文本文件、SQLite 数据库或任何您想要的文件)中。 Python 为 a built-in library for working with CSV files .

然后,您可以运行另一个脚本来获取数据、处理数据并生成 HTML 页面。由于每次都将重新创建 HTML 页面,因此您不必费心将新数据插入 HTML 中的正确位置。

将数据与表示分离是一种很好的做法,因为这样可以更轻松地在不同地方重用数据。在此示例中,如果您将数据保存在 CSV 文件中,您还可以使用电子表格应用程序打开它,并为您的雇主制作精美的图形和图表;)

关于Python file.write 在 html 标签内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25789956/

相关文章:

Python 模拟打开并检查关闭

python - 如何更改 tqdm 的条形大小

Django admin.py 过滤器中的 HTML 输入文本框

html - 为什么我的 IE6 条件样式表不工作?

python - 如何在python 2环境conda中安装keras模块?

python - Python 中的排名是什么?

python - 从 Google Cloud Storage 读取 CSV 文件到 Datalab 并转换为 pandas dataframe

javascript - FileReader readAsBinaryString() 到图像 DOM 元素

javascript - 如何使用 html 类从 html 页面检索数据?

php - Unescape html 以在 View 中正确显示