简体   繁体   English

此打印件的格式有什么问题? 为什么没有对齐?

[英]What's wrong in this print's formatting? Why is it not alligned?

While trying to solve an exercise, I tried to make the final print's exhibition a bit better by using tab (\t) inside the line.在尝试解决一个练习时,我试图通过在行内使用制表符 (\t) 使最终印刷品的展示效果更好一些。 I tried it in another stance and it made the final result look like a table.我换了个姿势试了一下,最后的结果看起来像一张桌子。

Should I ditch this idea?我应该放弃这个想法吗? What would be an alternative to improve this final line?有什么替代方法可以改进这最后一行?

#this first line was given by the teacher 
vendas_produtos = [('iphone', 558147, 951642), ('galaxy', 712350, 244295), ('ipad', 573823, 26964), ('tv', 405252, 787604), ('máquina de café', 718654, 867660), ('kindle', 531580, 78830), ('geladeira', 973139, 710331), ('adega', 892292, 646016), ('notebook dell', 422760, 694913), ('notebook hp', 154753, 539704), ('notebook asus', 887061, 324831), ('microsoft surface', 438508, 667179), ('webcam', 237467, 295633), ('caixa de som', 489705, 725316), ('microfone', 328311, 644622), ('câmera canon', 591120, 994303)] 


print('Produto\t\tVendas de 2019\t\tVendas de 2020\t\t% de crescimento')
for tupla_produtos in vendas_produtos:
    produto, vendas2019, vendas2020 = tupla_produtos
    if vendas2020 > vendas2019:
        crescimento = vendas2020 / vendas2019 - 1
        print(f'{produto}\t\t{vendas2019:,.2f}\t\t{vendas2020:,.2f}\t\t{crescimento:.1%}')

Note that I tried using the \t in both prints to try and make the final result aligned, like a table of sorts.请注意,我尝试在两个打印中都使用 \t 来尝试使最终结果对齐,就像各种表格一样。 The image shows what the result was like该图显示了结果

The code and its output代码及其output

You can use string formatting like this:您可以像这样使用字符串格式

vendas_produtos = [('iphone', 558147, 951642), ('galaxy', 712350, 244295), ('ipad', 573823, 26964), ('tv', 405252, 787604), ('máquina de café', 718654, 867660), ('kindle', 531580, 78830), ('geladeira', 973139, 710331), ('adega', 892292, 646016), ('notebook dell', 422760, 694913), ('notebook hp', 154753, 539704), ('notebook asus', 887061, 324831), ('microsoft surface', 438508, 667179), ('webcam', 237467, 295633), ('caixa de som', 489705, 725316), ('microfone', 328311, 644622), ('câmera canon', 591120, 994303)] 

print(f"%-20s %-20s %-20s %-20s" % ("produto", "Vendas de 2019", "Vendas de 2020", "% de crescimento") )

for tupla_produtos in vendas_produtos:
    produto, vendas2019, vendas2020 = tupla_produtos
    if vendas2020 > vendas2019:
        crescimento = vendas2020 / vendas2019 - 1
        print(f"%-20s %-20.2f %-20.2f %-20.1f" % (produto, vendas2019, vendas2020, crescimento) )

Output : Output :

produto              Vendas de 2019       Vendas de 2020       % de crescimento    
iphone               558147.00            951642.00            0.7                 
tv                   405252.00            787604.00            0.9                 
máquina de café      718654.00            867660.00            0.2                 
notebook dell        422760.00            694913.00            0.6                 
notebook hp          154753.00            539704.00            2.5                 
microsoft surface    438508.00            667179.00            0.5                 
webcam               237467.00            295633.00            0.2                 
caixa de som         489705.00            725316.00            0.5                 
microfone            328311.00            644622.00            1.0                 
câmera canon         591120.00            994303.00            0.7                 
print(f'{produto}\t\t{vendas2019:,.2f}\t\t{vendas2020:,.2f}\t\t{crescimento:.1%}')

The \t\t makes it's job and depending on the produto length you see the unaligned result. \t\t使它成为工作,并且根据produto长度,您会看到未对齐的结果。

Try import pprint and play with that, it should give you an help!尝试import pprint并使用它,它应该会给你帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM