Creare una tabella con Python

Vediamo come possiamo creare una tabella con Python in codice html, per evitare di scrivere il codice nel modo più semplice possibile.

Il codice per creare la tabella:

# simple table
from random import randrange
import os


def wrap(a, tag):
    "Wraps in <td> tag the a"
    tag1 = tag
    if tag == "table":
        tag1 = "table border=1"
    if tag == "td" and a.strip().replace(".", "").isdigit():
        print(a, "è un numero")
        tag1 = "td style=\"text-align:right\""
    return f"<{tag1}>{a}</{tag}>"


def split(tab):
    "Splits a multiline string in a list of items divided by comma for line"
    tab = tab.splitlines()
    for n, row in enumerate(tab):
        tab[n] = row.split(",")
    return tab


def table(tab):
    html = ''  # contain html
    for n, x in enumerate(tab):
        for a in x:
            html += wrap(a, "td")
        html += "<tr>"
    html = wrap(html, "table")
    return html


data = table(split("""
Impiegato, Performance, data
Rossi Mario, 1000, 1/2/2018
Baldo Franco, 2000, 1/2/2018
    """)[1:-1])

with open("Simple.html", "w", encoding="utf-8") as filehtml:
    filehtml.write(data)

os.system("Simple.html")

 

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.