Create an html table with python

import re

# lista di stringhe 
# ["riga1","riga2"...]
table = """
Titolo
Gennaio,10.600,ciao
Febbraio,9.800
Marzo,11.300
""".splitlines()[1:]

def tb(x):
	global x2
	x2 = []
	for l in x:
		x2.append(l.split(","))
	# x2 = [["Materie prime","42.000"],[....]]
	html = "<table border=1>"
	numcol = 0
	for n in x2:
		if numcol < len(l):
			numcol = len(n)
	for l in x2: # per ogni lista nella lista
		html += "<tr>"
		for n in l: # per ogni elemento di ogni listsa
			if len(l) == 1:
				html += "<td colspan="+ str(numcol) +">" + n + "</td>"
			else:
				if re.match("[0-9\s]",n):
					html += "<td align='right'>" + n + "</td>"
				else:
					html += "<td>" + n + "</td>"
	html +="</table>"
	print(html)



tb(table)

Risultato

Titolo
Gennaio10.600ciao
Febbraio9.800
Marzo11.300

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.