Sunday, April 19, 2009

Generate HTML table code using python


Input file:

$ cat data.txt
header1|header2:valueA|valueB
valueC|valueD
header3|header4|header5:valueE|valueF|valueG


Output required : The output should be a piece of HTML code such that the fields(| separated) in the LHS (: separated) become the table header(th) and RHS fields become the table data(td). If a line does not have the RHS table header portion, the fields(| separated) should just become table data. Graphically the output should be as shown below:




Python Code:

fp = open("data.txt", "rU")
lines = fp.readlines()
fp.close()

print "<html>"
print "<body bgcolor=\"white\">"
print "<table border=\"2\" cellspacing=\"0\" cellpadding=\"7\">"

def th(strn):
print "<tr><td></td></tr>"
print "<tr>"
fields=strn.split("|")
for field in fields:
print "<th>"+field+"</th>"
print "</tr>"

def td(strn):
print "<tr>"
fields=strn.split("|")
for field in fields:
print "<td>"+field+"</td>"
print "</tr>"

for line in lines:
f=line.split(":")
L=len(f)
if L==2:
th(f[0])
td(f[1])
else:
td(f[0])
print "</table>"
print "</body>"
print "</html>"


Executing the above script:

$ python gen_html.py
<html>
<body bgcolor="white">
<table border="2" cellspacing="0" cellpadding="7">
<tr><td></td></tr>
<tr>
<th>header1</th>
<th>header2</th>
</tr>
<tr>
<td>valueA</td>
<td>valueB
</td>
</tr>
<tr>
<td>valueC</td>
<td>valueD
</td>
</tr>
<tr><td></td></tr>
<tr>
<th>header3</th>
<th>header4</th>
<th>header5</th>
</tr>
<tr>
<td>valueE</td>
<td>valueF</td>
<td>valueG
</td>
</tr>
</table>
</body>
</html>


Related functions and concepts:
1) Python functions reader more

1 Comment:

Unknown said...

This is a very newbie piece of code and I am sure there can be very efficient way of doing this html generation in python; any suggestion is always appreciated, thanks