carte blanche
carte blanche

Reputation: 11476

Table creating gets messed up

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

Answers (3)

Crowman
Crowman

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

alecxe
alecxe

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

Mario Rossi
Mario Rossi

Reputation: 7799

Steps to debug the issue:

  1. Identify the conditions that cause the issue, especially dataset.
  2. Reduce. Remove information from the dataset until you have exactly what makes the program fail.
  3. Debug the program with only that information.
  4. Regress-test with the full dataset plus previous tests.

Upvotes: 0

Related Questions