重复打印一个列表

时间:2016-07-11 12:22:41

标签: python

我想反复打印一份清单。在我的程序中基本上发生的是我从 for 循环中的电子表格中获取一行数据并填充列表client = []。然后,我有一系列 if else 语句来确定是否打印值。然后我在每行的末尾清除client = []。这从上到下打印,但由于我想打印输出,我想并排打印每行数据的结果。

请注意,我的电子表格的每一行都包含与一个客户端相关的数据,我基本上想要为每个客户端打印一份列表。

client = []

for rowOfCellObjects in Millar_sheet['A2':'AA13']:
    for cellObj in rowOfCellObjects:
            client.append(cellObj.value)
            print(client[0]) #policy number
            print(client[9]) #license plate
    if client[12] != "Not Applicable":
        float(client[12])
        print('S.I. = ' + '$' + str("%0.2f" % client[12]))
    else:
        print('S.I. ' + client[12])
        print('Basic = ' + '$' + str("%0.2f" % client[13]))


client = []

这是输出的样子:

PP00041503
PCR 2703 
S.I. Not Applicable
Basic = $1000.00
Loading = 15.0%
Subtotal = $1150.00
SP = 10.0%, -$103.50
$1035.00
NCD = 20.0%, -$207.00
$828.00
Pre-tax Premium is $828.00
Premium Tax = +$49.68
$877.68
RS = +$100
Total = $977.68
--- END OF ROW ---
PP00041503
PCR 2703 
Basic = $1000.00
Loading = 15.0%
Subtotal = $1150.00
SP = 10.0%, -$103.50
$1035.00
NCD = 20.0%, -$207.00
$828.00
Pre-tax Premium is $828.00
Premium Tax = +$49.68
$877.68
RS = +$100
Total = $977.68
--- END OF ROW ---

我希望它看起来像这样:

PP00041503                          PP00041503
PCR 2703                            PCR 2703 
S.I. Not Applicable                 Basic = $1000.00
Basic = $1000.00                    Loading = 15.0%
Loading = 15.0%                     Subtotal = $1150.00
Subtotal = $1150.00                 SP = 10.0%, -$103.50
SP = 10.0%, -$103.50                $1035.00
$1035.00                            NCD = 20.0%, -$207.00
NCD = 20.0%, -$207.00               $828.00
$828.00                             Pre-tax Premium is $828.00
Pre-tax Premium is $828.00          Premium Tax = +$49.68 
Premium Tax = +$49.68               $877.68
$877.68                             RS = +$100
RS = +$100                          Total = $977.68
Total = $977.68                     --- END OF ROW ---
--- END OF ROW ---

3 个答案:

答案 0 :(得分:1)

我想你正在寻找这个:

print('a', end=' ')
print('b', end=' ')
print('c', end=' ')
print('d')
print('e', end='.')

OUTPUT: a b c d
OUTPUT: e.

如果您没有为结束指定参数,则会在您打印的每个字符串后添加换行符。当然,您可以使用除空格之外的其他字符。

如果这不是你想要的,那么给我们一个你想要的输出的例子可能是一个好主意。

修改

这有点乱,但应该有效。基本上,有一个列表( print_list ),它附加每行输出的列表( output )。 print_list 现在将是一个二维列表。循环显示y值和x值并打印元素。如果每行都有不同数量的要打印的元素,则添加try-except-clause。

client = []
print_list = []

for rowOfCellObjects in Millar_sheet['A2':'AA13']:
    output = []
    for cellObj in rowOfCellObjects:
            client.append(cellObj.value)
            output.append(client[0]) #policy number
            output.append(client[9]) #license plate
    if client[12] != "Not Applicable":
        float(client[12])
        output.append('S.I. = ' + '$' + str("%0.2f" % client[12]))
    else:
        output.append('S.I. ' + client[12])
        output.append('Basic = ' + '$' + str("%0.2f" % client[13]))
    print_list.append(output)


for x in range(len(max(print_list, key=len))):
    for y in range(len(print_list)):
        try:
            print(print_list[y][x], end=" ")
        except IndexError:
            pass
    print("")

答案 1 :(得分:1)

您可以将输出附加到字符串中并在末尾打印结果字符串。

基于帖子描述的示例代码:

output_str = '{} {}'.format(client[0], client[9])

if client[12] != "Not Applicable":
    output_str = output_str + ' S.I. = ' + '$' + str("%0.2f" % client[12])
else:
    output_str = output_str + ' S.I. ' + client[12]
    output_str = output_str + ' Basic = ' + '$' + str("%0.2f" % client[13])
print (output_str)

答案 2 :(得分:0)

我不确定我是否正确理解您的问题,但您可以按照以下方式并排打印:

print "one", "two"
print "one",
print "two"

您还可以将数据保存在pandas数据框(df)中并使用:

print df