I hope today you have a great mood, but let's get to the essence of the question.

There is a list:

relations_table = [[0 for i in range(0, len(sounds))] for j in range(0, len(sounds))] for i in relations: relations_table[i.left_sound_id-1][i.right_sound_id-1] = i.weight 

It looks like this:

 [[25, 98, 40, 17, 66, 65, 41, 11, 70, 58], [89, 93, 35, 89, ... 

How to fill this data in the html table, where the index denotes the row and column of a cell with a value?

If you can sample code. Thank.

    2 answers 2

    The simplest solution:

     relations_table = [[25, 98, 40, 17, 66, 65, 41, 11, 70, 58], [89, 93, 35, 89, 40, 17, 66, 65, 41, 99]] html = '<table>' for row in relations_table: html += '<tr>' for value in row: html += '<td>{}</td>'.format(value) html += '</tr>' html += '</table>' print(html) 

    Another option that is more convenient for complex data is the jinja template engine :

     # pip install jinja2 import jinja2 template = jinja2.Template(''' <table cellspacing="2" border="1" cellpadding="5"> {% for row in relations_table %} <tr> {% for value in row %} <td>{{ value }}</td> {% endfor %} </tr> {% endfor %} </table> ''') html = template.render(relations_table=relations_table) print(html) 

    This is what the table in the browser will look like:

    enter image description here


    You can create more complicated things. An example of html page generation via jinja2

    • Thank you very much, it helped a lot - DmitriyLeaf
    • Please contact :) - gil9red

    You can use the Pandas module:

     import pandas as pd html = pd.DataFrame(relations_table).to_html(header=None, index=False, border=2) with open('d:/temp/a.html', 'w') as f: f.write(html) 

    Result:

    enter image description here

    • 2
      Pandas is just a super module for working with tablets :) - gil9red
    • @ gil9red, it's hard not to agree with this :) - MaxU