如何以表格格式打印不可预测长度的字段?

时间:2015-08-20 04:05:23

标签: python string-formatting

我想将线条分割成字段并将它们打印整齐排列。我不知道名字有多长,所以我不能使用像其他Stack Overflow问题中给出的固定格式。

这是我想要的输出:

Simpson, Bartholomew Homer  12345 CSEE       £25000
Jackson, Michael            15675 History    £34000
Clown, Krusty the           56746 Economics  £67000

这是我到目前为止编写的代码:

def printFormat(details):
    details = details.split()
    lastname = details[-1]
    firstnames = " ".join(details[3:-1])
    name = ", ".join([lastname, firstnames])

    ID_Subject = " ".join(details[0:2])
    money = details[2]

    print "%s,%s    %s    %s" % (lastname,firstnames,ID_Subject,money)

0 个答案:

没有答案