pythonhtmlhtml-tableimport-from-csv

Converting CSV to HTML Table in Python


I'm trying to take data from a .csv file and importing into a HTML table within python.

This is the csv file https://www.mediafire.com/?mootyaa33bmijiq

Context:
The csv is populated with data from a football team [Age group, Round, Opposition, Team Score, Opposition Score, Location]. I need to be able to select a specific age group and only display those details in separate tables.

This is all I've got so far....

infile = open("Crushers.csv","r")

for line in infile:
    row = line.split(",")
    age = row[0]
    week = row [1]
    opp = row[2]
    ACscr = row[3]
    OPPscr = row[4]
    location = row[5]

if age == 'U12':
   print(week, opp, ACscr, OPPscr, location)

Solution

  • Before you begin printing the desired rows, output some HTML to set up an appropriate table structure.

    When you find a row you want to print, output it in HTML table row format.

    # begin the table
    print("<table>")
    
    # column headers
    print("<th>")
    print("<td>Week</td>")
    print("<td>Opp</td>")
    print("<td>ACscr</td>")
    print("<td>OPPscr</td>")
    print("<td>Location</td>")
    print("</th>")
    
    infile = open("Crushers.csv","r")
    
    for line in infile:
        row = line.split(",")
        age = row[0]
        week = row [1]
        opp = row[2]
        ACscr = row[3]
        OPPscr = row[4]
        location = row[5]
    
        if age == 'U12':
            print("<tr>")
            print("<td>%s</td>" % week)
            print("<td>%s</td>" % opp)
            print("<td>%s</td>" % ACscr)
            print("<td>%s</td>" % OPPscr)
            print("<td>%s</td>" % location)
            print("</tr>")
    
    # end the table
    print("</table>")