Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My python list is being printed entirely in one row in a HTML table

I am making a python script which makes a html table with a list (Without a module), but it keeps printing the entire list in one row instead of wrapping it like I need. My list is data = ['1', '2', '3', ... , '30'].
Im trying to get it to 5 cells in each row (not counting the side header) but I keep getting the entire list being printed in every row. Here is a blank table for a bit of reference.

My code,

classes = {"ILA":'#d0e2f3', "Math":'#f5cacc', "Science":'#d9ead3', "Social<br>Studies":'#ffe598', "Academy<br>Connect!":
    '#dad2e9', "Elective":'#f9cb9c'}

file = open("/Users/EhMehMan/PythonProjects/agenda/output.html", "w")

table = "<h3>Date:__________________________________________________________</h3>\n<head>\n<style>table, th, td" \
        " {\nborder: 3px solid black;\nborder-collapse: collapse;\n}\n</style>\n</head>\n<table style='width:100%'>\n"

#-----------------------------------------------------------------------------------

table += "  <tr>\n"
table += "    <th>{0}</th>\n".format(" ")
for z in days:
    table += "    <th style = 'height:74px;width:350px'>{text}</th>\n".format(text=z)
table += "  </tr>\n"


for x in classes.keys():
    table += "  <tr>\n"
    table += "    <th style='height:100px;width:100px' bgcolor='{color}'>{classes}</th>\n".format(color=classes[x],classes=x)
    for element in data:
        table += "    <td>{cell}</td>\n".format(cell = element)
    table += "  </tr>\n"
table += "</table>"
#-----------------------------------------------------------------------------------

file.writelines(table)

Any help would be great!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The loop for element in data: iterates over the entire dataset every time. You need to adjust it to run in chunks of size len(days).

There are a couple of ways of doing that. The simplest one I can think of is to insert a </tr><tr> every n elements:

for index, element in enumerate(data):
    if index % len(days) == 0:
        table += "  </tr>\n  <tr>"
    table += "    <td>{cell}</td>\n".format(cell = element)
table += "  </tr>\n"

A faster way to do what you want would be to write to the file directly instead of accumulating a giant string in memory. Also, operate on files using a with block, so resources get freed up correctly in case of error. So for the example above:

with open("/Users/EhMehMan/PythonProjects/agenda/output.html", "w") as file:
    file.write('<h3>Date:__________________________________________________________</h3>\n<head>\n')
    file.write('<style>table, th, td {\nborder: 3px solid black;\nborder-collapse: collapse;\n}\n</style>\n</head>\n<table style='width:100%'>\n')

    ...

        file.write('  <tr>\n')
        file.write(f'    <th style="height:100px;width:100px" bgcolor="{classes[x]}">{x}</th>\n')
        for index, element in enumerate(data):
            if index % len(days) == 0:
                file.write('  </tr>\n  <tr>')
            file.write(f'    <td>{element}</td>\n')
        file.write('  </tr>\n')
    file.write('</table>')

Notice that formatting gets a lot easier with f-strings.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading