python - 如何在 Python 中将输入更改为表单格式?
问题描述
我有这样的数据输入:
day_i = [“Bob: 1200”, “Alice: 2500”, “Celia: 110”, etc…]
我希望我的输出看起来像一个表格并计算数字的总和,如下所示:
Customer Total purchase
Alice 100
Bob 120
Celia 110
解决方案
假设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]))
在使用此代码之前,我建议您在此处阅读有关 Python 的 string.Formatter 类的更多信息。
推荐阅读
- node.js - 如果资源已经被锁定会发生什么?
- node.js - 有条件地导入Vue中条件存在的文件
- python - 如何在python中为一天的开始设置自定义时间?
- velo - 如何获取帖子作者数据以将其显示在 html 框中?
- python - Tkinter 将 Intvar() 与函数和按钮链接
- python - 为什么我的 projecteuler.net 问题 12 的代码不起作用?
- javascript - d3 放大特定国家(硬编码)
- angular - 如何在 kendo ui 网格中动态添加/渲染 Kendo UI 控件 - Angular
- python - 使用 Pyinstaller 时找不到隐藏的导入 Tensorflow 包
- javascript - searchEntry() laravel 或 php 常量函数?