Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6669389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:08:38+00:00 2026-05-26T03:08:38+00:00

I’m having a difficulty formatting a table in html through python. I’m using for-loops

  • 0

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']
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:08:39+00:00Added an answer on May 26, 2026 at 3:08 am

    Errors

    • You should not use table rows (tr elements) for columns!
    • You do not close open headers (th elements)
    • You should use table headers in the head onlym not in the content rows!

    Valid table structure

        <table>
            <tr>
                <th>Year</th>
                <th>Highest Grossing Film</th>
                <th>Most Similar</th>
                <th>2nd Most Similar</th>
                <th>3rd Most Similar</th>
            </tr>
    
        <!-- create rows in a loop -->
        <tr>
            <td> ... </td>
            <td> ... </td>
            <td> ... </td>
            <td> ... </td>
            <td> ... </td>
        </tr>
        <!-- loop this ^^ -->
    
    </table>
    

    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:

    print "<table>"
    
    print "<tr>"
        // print your caption row
        print "<th> Year </th>"
        print "<th> Highest Grossing Film </th>"
        print "<th> Most Similar </th>"
        print "<th> 2nd Most Similar </th>"
        print "<th> 3rd Most Similar </th>"
    print "</tr>"
    
    // loop data
    for row_data in data:
        print "<tr>"
    
        // print data (maybe: row_data[0] == year, row_data[1] == movie name etc.)
        for cell_data in row_data:
            print "<td>"
            print cell_data
            print "</td>"
    
        print "</tr>"
    
    print "</table>"
    

    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:

    for c in sorted(max_films):
        print "<tr><td>"
        print max_films[c]
        print "</td>"
    
    for c in max_list1:
        print "<td>"
        print c
        print "</td></tr>"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.