[英]python Printing a list in a tabular format without modules
I have a list called entryList
which begins empty, but as the user is prompted for input this input is added to the list.我有一个名为
entryList
的列表,它开始为空,但是当提示用户输入时,此输入被添加到列表中。 After 2 runs the list will look something like this: [2, 1234, 'E', 2.0, 2.71, 6, 0800, 'U', 2.34, 20.89]
with each iteration of asking for input adding 5 values to the list.运行 2 次后,列表将如下所示:
[2, 1234, 'E', 2.0, 2.71, 6, 0800, 'U', 2.34, 20.89]
每次请求输入的迭代都会向列表中添加 5 个值。 When this list is printed, I need it to look something like this:打印此列表时,我需要它看起来像这样:
EntryNo PumpNo Time FType LPrice FAmount
---------------------------------------------
1 2 1234 E 2.00 2.71
2 6 0800 U 2.34 20.89
How can I get the list to print like this, with the EntryNo column increasing by 1 for each new row added, without importing any modules?如何在不导入任何模块的情况下像这样打印列表,EntryNo 列每添加一个新行就增加 1?
values = [2, '1234', 'E', 2.0, 2.71,
6, '0800', 'U', 2.34, 20.89]
cols = ["EntryNo", "PumpNo", "Time", "FType", "LPrice", "FAmount"]
print(*cols)
print("-" * len(" ".join(cols))) # optional
for idx, i in enumerate(range(0, len(values), 5), start=1):
print("{:<7d} {:<6d} {:<4s} {:<5s} {:<6.2f} {} ".format(idx, *values[i:i + 5]))
EntryNo PumpNo Time FType LPrice FAmount
----------------------------------------
1 2 1234 E 2.00 2.71
2 6 0800 U 2.34 20.89
pandas
pandas
You can use pandas, you'll easily have an nice output, with to_markdown
for example你可以使用熊猫,你会很容易得到一个很好的输出,例如
to_markdown
import pandas as pd
values = [2, '1234', 'E', 2.0, 2.71,
6, '0800', 'U', 2.34, 20.89]
df = pd.DataFrame([[idx, *values[i:i + 5]] for idx, i in enumerate(range(0, len(values), 5), start=1)],
columns=["EntryNo", "PumpNo", "Time", "FType", "LPrice", "FAmount"])
print(df.to_markdown(index=False))
| EntryNo | PumpNo | Time | FType | LPrice | FAmount |
|----------:|---------:|-------:|:--------|---------:|----------:|
| 1 | 2 | 1234 | E | 2 | 2.71 |
| 2 | 6 | 0800 | U | 2.34 | 20.89 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.