0

我试图获得一个左对齐的输出..但我一直遇到对齐问题。这是我的代码:

def print_report(symbol:str,strategy:str, alist1:list, alist2:list, alist3:list, alist4:list):
    print('\nSYMBOL:   ', symbol.upper())
    print('STRATEGY: ', strategy)
    print('\n''DATE            CLOSING        INDICATOR        SIGNAL')
    for i in range(0,len(alist1)):
        print('{0:<0}{1:>13}{2:>15}{3:>15}'.format(alist1[i],alist2[i],alist3[i],alist4[i]))

输出应如下所示:

DATE          CLOSING     INDICATOR       SIGNAL
2013-10-01     887.00        0.00               
2013-10-02     887.99        0.00               
2013-10-03     876.09        0.00               
2013-10-04     872.35        0.00               
2013-10-07     865.74      877.83         SELL
2013-10-08     853.67      871.17         SELL
2013-10-09     855.86      864.74         SELL
2013-10-10     868.24      863.17          BUY
2013-10-11     871.99      863.10          BUY
2013-10-18    1011.41      911.27          BUY
2013-10-21    1003.30      936.71          BUY

有没有更简单的方法来获得输出?或正确对齐它们?

4

1 回答 1

0

以与数据行相同的格式打印标题。

def print_report(symbol:str, strategy:str,
                 dates:list, closings:list, indicators:list, signals:list):
    fmt = '{0[0]:<0}{0[1]:>13}{0[2]:>15}{0[3]:>15}'
    print()
    print('SYMBOL:   ', symbol.upper())
    print('STRATEGY: ', strategy)
    print()
    print(fmt.format(('DATE', 'CLOSING', 'INDICATOR', 'SIGNAL')))
    for d in zip(dates, closings, indicators, signals):
        print(fmt.format(d))

如果您愿意,您可以为标题使用不同的格式,但重点是,也可以使用format标题的方法。

于 2013-11-08T21:18:02.907 回答