2

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>"

  ............
  ............
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
carte blanche
  • 10,796
  • 14
  • 46
  • 65

3 Answers3

1

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.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I cannot change the code now..need to get it working this way..complete code is http://pastie.org/8263783 ..anything you can tell from the code – carte blanche Aug 23 '13 at 21:05
  • @user2125827 I bet the data you are inputting there has sometimes some characters that needs to be escaped. – alecxe Aug 23 '13 at 21:11
  • @user2125827 and if you'll switch to `mako` - it'll make escaping for you :) Don't reinvent the wheel. – alecxe Aug 23 '13 at 21:12
0

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.
Mario Rossi
  • 7,651
  • 27
  • 37
0

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?

Crowman
  • 25,242
  • 5
  • 48
  • 56