【问题标题】:aligning, spacing in python 3python 3中的对齐,间距
【发布时间】:2014-03-23 13:07:08
【问题描述】:

我可以做些什么来对齐这段代码中的所有列?是正确的还是...?

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

【问题讨论】:

    标签: python python-3.x string-formatting spacing


    【解决方案1】:

    不要使用标签,使用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))
    

    12 更改为每列的最大字段大小,您就可以了。

    事实上,虽然它的可读性较差:

    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))
    

    【讨论】:

      猜你喜欢
      • 2012-05-24
      • 2012-03-09
      • 2012-09-15
      • 2012-06-18
      • 1970-01-01
      • 2012-01-29
      • 1970-01-01
      • 2014-04-09
      相关资源
      最近更新 更多