I’m having a difficulty formatting a table in html through python. I’m using for-loops from previous information, and the first two columns have been formatted, but I’m having difficulty placing certain values in the right place.
the part of my code determining the second column:
for c in sorted(max_films):
print "<tr><th>"
print c
print "<td>"
print max_films[c]
print "</td></tr>"
should be producing the third
for c in max_list1:
print "<tr><th>"
print "<td>"
print c
print "</td></tr>"
This is the table it is producing:
- Year Highest Grossing Film Most Similar 2nd Most Similar 3rd Most Similar
- 2000 The Sixth Sense
- 2001 Star Wars: Episode I – The Phantom Menace
- 2002 Harry Potter and the Sorcerer’s Stone
- 2003 The Lord of the Rings: The Two Towers
- 2004 The Lord of the Rings: The Return of the King
- 2005 Star Wars: Episode III – Revenge of the Sith
- 2006 Pirates of the Caribbean: Dead Man’s Chest
- 2007 Pirates of the Caribbean: At World’s End
- (blank)The Perfect Storm
- (blank)Planet of the Apes
- (blank)The Lord of the Rings: The Fellowship of the Ring
- (blank)Spirited Away
- (blank)Collateral
- (blank)Troy
- (blank)The Chronicles of Narnia: The Lion the Witch and the Wardrobe
- (blank)300
The films(Perfect Storm – 300) should be aligned in the third column instead of under the second. How can i fix this??
full html code:
print "Content-Type: text/html"
print ""
print "<html>"
print "<body>"
print "<table border=1>"
print "<tr>"
print "<th><font color=black>Year</font></th>"
print "<th><font color=blue>Highest Grossing Film</font></th>"
print "<th><font color=red>Most Similar</font></th>"
print "<th><font color=red>2nd Most Similar</font></th>"
print "<th><font color=red>3rd Most Similar</font></th>"
print "<th><font color=red>4th Most Similar</font></th>"
print "</tr>"
for c in sorted(max_films):
print "<tr><th>"
print c
print "<td>"
print max_films[c]
print "</td></tr>"
for c in max_list1:
print "<tr><th>"
print "<td>"
print c
print "</td></tr>"
print "</body>"
print "</html>"
sorted(max_films) = ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007']
max_list1 = ['The Perfect Storm', 'Planet of the Apes', 'The Lord of the Rings: The Fellowship of the Ring', 'Spirited Away', 'Collateral', 'Troy', 'The Chronicles of Narnia: The Lion the Witch and the Wardrobe', '300']
Errors
Valid table structure
See sample: http://jsfiddle.net/n2w5D/
General solution
You cannot loop over columns when creating tables, because you need to write all table cells of a row into one row. The structure of a table is based on rows as parent element not on columns!
You will need to have the data for each row to procude something like this:
Solution for your case
After you posted your full code, I think you didn’t mean columns, but rows!
I think this is what you want: