繁体   English   中英

html标签中的Python file.write

[英]Python file.write within html tags

我刚开始使用Python,我的第一个脚本是一个非常简单的工作日志。 我运行它,当我完成工作时我按Enter键它将工作时间放入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/

它肯定不是最高级别的并且有一些问题,但它比我来到这里的那块垃圾要好得多:)

非常感谢你。

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

您应该考虑将数据(开始时间和结束时间)存储到CSV文件(或文本文件,SQLite数据库或任何您想要的任何内容)中,而不是直接记录到HTML文件。 Python作为用于处理CSV文件的内置库

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

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

不要通过使用字符串连接手动构造HTML来重新发明轮子。 这使得事情变得不那么可读,明确,更复杂,难以维护,容易出错。 有专门的工具,这将使它更容易和愉快。

考虑使用模板引擎,如jinja2mako

基本上,您将使用占位符创建HTML模板,在渲染时将使用占位符填充数据。

使用mako模板引擎的示例。

  • 考虑一下名为template.html其中包含以下内容:

     <!DOCTYPE html> <html> <head> <title>${title}</title> </head> <body> <span> Work started: ${work_time} </span> </body> </html> 
  • 这是您的渲染代码可能是这样的:

     from datetime import date from mako.template import Template template = Template(filename='template.html') title = 'Test page' work_time = date.today() print template.render(title=title, work_time=work_time) 

它打印:

<!DOCTYPE html>
<html>
<head>
    <title>Test page</title>
</head>
<body>
    <span>
        Work started: 2014-09-11
    </span>
</body>
</html>

或者,您也可以在Python代码中按标签构造HTML标记。

使用BeautifulSoup示例:

from bs4 import BeautifulSoup


soup = BeautifulSoup()
html = soup.new_tag(name='html')
body = soup.new_tag(name='body')

span = soup.new_tag(name="span")
span.append(soup.new_string('Work started:'))

body.append(span)
html.append(body)
soup.append(html)

print soup.prettify()

打印:

<html>
 <body>
  <span>
   Work started:
  </span>
 </body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM