我试图得到一个左对齐的输出..但我不断得到对齐问题。这是我的代码:
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
有更简单的方法来获得输出吗?或正确对齐它们?
答案 0 :(得分: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
方法。