简体   繁体   English

如何在 Python 中将输入更改为表单格式?

[英]How to change the input into form format in Python?

I have data input like this:我有这样的数据输入:

day_i = [“Bob: 1200”, “Alice: 2500”, “Celia: 110”, etc…]

I want my output to look like a form and calculate the sum of number, like so:我希望我的输出看起来像一个表格并计算数字的总和,如下所示:

Customer    Total purchase
Alice       100
Bob         120
Celia       110

Assuming day_i is meant to be a proper Python dictionary, this is what I think you're asking for.假设day_i是一个合适的 Python 字典,这就是我认为你要求的。

day_i = {                                                                                                                                          
    'Bob': 1200, 
    'Alice': 2500, 
    'Celia': 110 
}

print("{:15s}{:15s}".format("Customer", "Total Purchase"))
for person in day_i:
    print("{:15s}{:<15d}".format(person, day_i[person]))

Before using this code, I suggest you read more about Python's string.Formatter class here .在使用此代码之前,我建议您在此处阅读有关 Python 的 string.Formatter 类的更多信息。

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

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