gpt4 book ai didi

python - python 3中的对齐、间距

转载 作者:行者123 更新时间:2023-12-01 05:20:02 26 4
gpt4 key购买 nike

我该怎么做才能对齐此代码中的所有列?这是正确的还是......?

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

最佳答案

不要使用制表符,使用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))

关于python - python 3中的对齐、间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22587229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com