A simple way to display a list of lists/tuples as a table in your browser ...
# make_html_table.py
# create an html table from a data list
def make_html_table(mylist):
"""
use a list of data tuples and create the html code
of a html formatted table containing the data items
"""
rows = ['<td>'+'</td><td>'.join(xx)+'</td>'+'\n' for xx in mylist]
table = '<table border=2><tr>'
table += '<tr>'.join(rows)
table += '</tr></table>'
return table
# a list of (name, age, weight) tuples (or lists)
# the first tuple is the header
data_list = [
('Name', 'Age', 'Weight'),
('Sarah Gellar', '26', '121'),
('Alec Baldwin', '47', '214'),
('Mandy Moore', '22', '135'),
('Matthew Goode', '29', '167'),
('Amanda Bynes', '19', '112'),
('James Kirk', '24', '175')
]
html_table = make_html_table(data_list)
print(html_table) # test
# save as html file and show in your browser
with open('html_table.htm', 'w') as fout:
fout.write(html_table)
You can use this simple code to show the above html file in your web browser ...
# use Python's webbrowser module to show an html code file
import webbrowser
# open your web browser to run the html file
webbrowser.open('html_table.htm')