Reputation: 11476
I am trying to create a table using the below HTML code in python,it works for most instances but for some cases the table gets messed up like below,please see screen shot..any inputs on what is wrong?how to debug it?any workarounds to fix it?really appreciate any inputs
HTML source for messed up row: 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>"
............
............
Upvotes: 2
Views: 138
Reputation: 25908
There are many, many problems with your HTML, but here's the most obvious one causing your issue at line 19:
<td style='padding:.75pt .75pt .75pt .75pt'><table!></td>
What's that <table!>
"tag" doing there? Did you read the HTML it gave you before asking your question?
Upvotes: 0
Reputation: 473853
Constructing html this way in python is not readable at all and difficult to maintain. Switch to a template engine like mako:
from mako.template import Template
print Template("hello ${data}!").render(data="world")
Define an html file with your message body template, fill it with data via render
and get the message as a string:
from mako.template import Template
mytemplate = Template(filename='body.html')
print mytemplate.render(data=data)
Trust me, this will make your life easier and sleep more peaceful.
Possible reason of the messed up HTML is that the data you are inserting into the html contains some characters (<
, <
, &
) that should be escaped. Consider calling cgi.escape()
on every item you are inserting. Also see: What's the easiest way to escape HTML in Python?
Again, escaping works out-of-the-box in most of the template engines out there.
Hope that helps.
Upvotes: 1
Reputation: 7799
Steps to debug the issue:
Upvotes: 0