我正在尝试格式化print语句以对齐特定列。在
目前我的输出是:0 - Rusty Bucket (40L bucket - quite rusty) = $0.00
1 - Golf Cart (Tesla powered 250 turbo) = $195.00*
2 - Thermomix (TM-31) = $25.50*
3 - AeroPress (Great coffee maker) = $5.00
4 - Guitar (JTV-59) = $12.95
我想要的输出是:
^{pr2}$
以下是我当前用于打印的代码:def list_items():
count = 0
print("All items on file (* indicates item is currently out):")
for splitline in all_lines:
in_out = splitline[3]
dollar_sign = "= $"
daily_price = "{0:.2f}".format(float(splitline[2]))
if in_out == "out":
in_out = str("*")
else:
in_out = str("")
print(count, "- {} ({}) {}{}{}".format(splitline[0], splitline[1], dollar_sign, daily_price, in_out))
count += 1
我尝试过使用以下格式:print(count, "- {:>5} ({:>5}) {:>5}{}{}".format(splitline[0], splitline[1], dollar_sign, daily_price, in_out))
但始终无法让一个列对齐。任何帮助或建议将不胜感激!我也在使用python3.x
需要注意的是,我使用元组来包含所有信息,all_lines是主列表。信息最初是从csv读取的。为糟糕的命名约定道歉,尝试先处理功能。在
很抱歉,如果其他地方有人回答这个问题,我已经试着找过了。在
编辑:这是我的所有行使用的代码import csv
open_file = open('items.csv', 'r+')
all_lines = []
for line in open_file:
splitline = line.strip().split(',')
all_lines.append((splitline[0], splitline[1], splitline[2], splitline[3]))
以下是csv文件信息:Rusty Bucket,40L bucket - quite rusty,0.0,in
Golf Cart,Tesla powered 250 turbo,195.0,out
Thermomix,TM-31,25.5,out
AeroPress,Great coffee maker,5.0,in
Guitar,JTV-59,12.95,in