简体   繁体   中英

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 = {                                                                                                                                          
    '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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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