繁体   English   中英

表创建陷入困境

[英]Table creating gets messed up

我正在尝试使用下面的HTML代码在python中创建表,它适用于大多数情况,但在某些情况下,该表会像下面那样混乱,请查看屏幕截图。任何关于输入错误的信息如何调试?任何解决方法来解决它?真的很感谢任何输入

混乱行的HTML源代码: http//pastie.org/8263837

  ...........
  ...........
  GerritMailBody = GerritMailBody + "<td>" + GerritInfo['TargetName'].rstrip('\n') + "</td>"
  GerritMailBody = GerritMailBody + "<td>" + GerritInfo['Assignee'].rstrip('\n') + "</td>"
  usernames.append(GerritInfo['Assignee'].rstrip('\n'))

  #Below is the block that is getting messed up
  GerritMailBody = GerritMailBody + "<td height=\"150\%\">"
  GerritMailBody = GerritMailBody + "<table>"  
  for item in GerritInfo['GerritUrl']:
    GerritMailBody = GerritMailBody + "<tr>"
    GerritMailBody = GerritMailBody + "<td>"
    GerritMailBody = GerritMailBody + item.rstrip('\n/') + "<br>"
    GerritMailBody = GerritMailBody + "</td>"
    GerritMailBody = GerritMailBody + "</tr>"

  GerritMailBody = GerritMailBody + "</table>"
  GerritMailBody = GerritMailBody + "</td>"

  ............
  ............

在python中以这种方式构造html根本不可读,并且难以维护。 切换到像mako这样的模板引擎:

from mako.template import Template
print Template("hello ${data}!").render(data="world")

使用您的消息正文模板定义一个html文件,通过render填充数据并以字符串形式获取消息:

from mako.template import Template

mytemplate = Template(filename='body.html')
print mytemplate.render(data=data)

相信我,这将使您的生活更轻松,睡眠更安宁。


弄乱HTML的可能原因是,您要插入html的数据包含一些应该转义的字符( <<& )。 考虑在要插入的每个项目上调用cgi.escape() 另请参阅: 在Python中转义HTML的最简单方法是什么?

同样,转义在大多数模板引擎中都是开箱即用的。

希望能有所帮助。

调试问题的步骤:

  1. 确定导致问题的条件,尤其是数据集。
  2. 降低。 从数据集中删除信息,直到您确切地知道什么使程序失败为止。
  3. 仅使用该信息调试程序。
  4. 使用完整数据集以及先前的测试进行回归测试。

HTML有很多很多问题,但是这是第19行引起您问题的最明显的问题:

<td style='padding:.75pt .75pt .75pt .75pt'><table!></td>

<table!> “标签”在那里做什么? 在问问题之前,您是否阅读过它给您的HTML?

暂无
暂无

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

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