Question

What can I do to align all columns in this code?, is that correct or...?

import urllib.request
from re import findall

def determinarLlegadas(numero):
    llegadas = urllib.request.urlopen("http://...")
    llegadas = str (llegadas.read())
    llegadas = findall ('<font color="Black" size="3">(.+?)</font>',llegadas)
    print ('\t','VUELO ','\t','AEROLINEA','\t','PROCEDENCIA','\t','FECHA ','\t',' HORA ','\t','ESTADO','\t','PUERTA')
    a = 0
    numero = numero * 7
    while numero > a:
        print ('\t',llegadas[a+0],'\t',llegadas[a+1],'\t',llegadas[a+3],'\t',llegadas[a+3],'\t',llegadas[a+4],'\t',llegadas[a+5],'\t',llegadas[a+6])
        a = a + 7
Was it helpful?

Solution

Don't use tabs, use string formatting.

...
print("{:12}{:12}{:12}{:12}{:12}{:12}{:12}".format(
      "VUELO","AEROLINEA","PROCEDENCIA","FECHA","HORA","ESTADO","PUERTA"))
print("{:12}{:12}{:12}{:12}{:12}{:12}{:12}".format(*llegadas))

Change the 12 to the maximum field size for each column, and you're golden.

In fact, though it's less readable:

COLSIZE = 12
# Maybe COLSIZE = max(map(len,llegadas))+1
NUMCOLS = 7
formatstring = "{}{}{}".format("{:",COLSIZE,"}")*NUMCOLS
# {:COLSIZE}*NUMCOLS

headers = ["VUELO","AEROLINEA","PROCEDENCIA","FECHA","HORA","ESTADO","PUERTA"]

print(formatstring.format(*headers))
print(formatstring.format(*llegadas))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top