python - 在 Python 中将列中的文本对齐为收据
问题描述
我正在尝试在同一位置显示不同的列,以便更具可读性
orders = [
(7306010, '06-01-2020', 'Lenovo V145 15.6" FHD Laptop AMD A9-9425, 8GB RAM, 256GB SSD, Black', 'IntelliWorld', '£444.99', 1, 'Delivered'),
(7224010, '24-12-2019', 'Samsung Galaxy M30s SIM Free Smartphone', 'DigiTec Limited', '£249.00', 4, 'Delivered'),
(7224010, '24-12-2019', 'Nikon Digital DSLR Camera Body (Black)', 'Orinoco UK', '£721.00', 1, 'Delivered'),
(7130010, '30-11-2019', 'Nintendo Switch', 'Orinoco UK', '£329.99', 6, 'Delivered'), (7102000, '02-11-2019', 'Huawei Y6 32GB 6.09 inch Smartphone', 'DigiTec Limited', '£107.50', 2, 'Delivered'),
(6731010, '31-07-2019', 'Numskull Multi-Format Pro Steering Wheel', 'Orinoco UK', '£69.99', 3, 'Delivered'),
(7304010, '04-01-2019', 'Canon DSLR Camera (Black)', 'Orinoco UK', '£249.00', 12, 'Delivered')]
print("Order ID\tOrder Date\t\tProduct Description\t\tSeller Name\t\tQuantity\t\tPrice")
for order in orders:
order_id = order[0]
order_date = order[1]
prod_descrip = order[2]
sel_name = order[3]
quantity = order[4]
price= order[5]
print("{0}\t\t{1}\t\t{2}\t{3}\t{4}\t{5}\t".format(order_id,order_date,prod_descrip,sel_name,quantity,price))
这是我的输出:
Order ID Order Date Product Description Seller Name Quantity Price
7306010 06-01-2020 Lenovo V145 15.6" FHD Laptop AMD A9-9425, 8GB RAM, 256GB SSD, Black IntelliWorld £444.99 1
7224010 24-12-2019 Samsung Galaxy M30s SIM Free Smartphone DigiTec Limited £249.00 4
7224010 24-12-2019 Nikon Digital DSLR Camera Body (Black) Orinoco UK £721.00 1
7130010 30-11-2019 Nintendo Switch Orinoco UK £329.99 6
7102000 02-11-2019 Huawei Y6 32GB 6.09 inch Smartphone DigiTec Limited £107.50 2
6731010 31-07-2019 Numskull Multi-Format Pro Steering Wheel Orinoco UK £69.99 3
7304010 04-01-2019 Canon DSLR Camera (Black) Orinoco UK £249.00 12
我试图让它更像这样(点击)
任何帮助将不胜感激
解决方案
避免重复相同的 ID 和日期
- 您需要跟踪以前的 ID,并检查它是否
print
基于该ID 进行更改
- 您需要跟踪以前的 ID,并检查它是否
获得良好的列格式
- 您需要在格式字符串中指定一些大小,例如
{:70s}
- 您需要在格式字符串中指定一些大小,例如
headers = ['Order ID', 'Order Date', 'Product Description', 'Seller Name', 'Price', 'Quantity', 'Status']
print("{}\t{}\t\t{:70s}\t{:20s}\t{}\t{}\t{}\n".format(*headers))
prev_id = ""
for order in orders:
if prev_id == order[0]:
print("\t\t\t\t\t{:70s}\t{:20s}\t{}\t{:>8d}\t{}".format(*order[2:]))
else:
print("{}\t\t{}\t\t{:70s}\t{:20s}\t{}\t{:>8d}\t{}".format(*order))
prev_id = order[0]
Order ID Order Date Product Description Seller Name Price Quantity Status
7306010 06-01-2020 Lenovo V145 15.6" FHD Laptop AMD A9-9425, 8GB RAM, 256GB SSD, Black IntelliWorld £444.99 1 Delivered
7224010 24-12-2019 Samsung Galaxy M30s SIM Free Smartphone DigiTec Limited £249.00 4 Delivered
Nikon Digital DSLR Camera Body (Black) Orinoco UK £721.00 1 Delivered
7130010 30-11-2019 Nintendo Switch Orinoco UK £329.99 6 Delivered
7102000 02-11-2019 Huawei Y6 32GB 6.09 inch Smartphone DigiTec Limited £107.50 2 Delivered
6731010 31-07-2019 Numskull Multi-Format Pro Steering Wheel Orinoco UK £69.99 3 Delivered
7304010 04-01-2019 Canon DSLR Camera (Black) Orinoco UK £249.00 12 Delivered
使用pandas
import pandas as pd
headers = ['Order ID', 'Order Date', 'Product Description', 'Seller Name', 'Price', 'Quantity', 'Status']
df = pd.DataFrame(orders, columns=headers)
print(df.to_markdown())
| | Order ID | Order Date | Product Description | Seller Name | Price | Quantity | Status |
|---:|-----------:|:-------------|:--------------------------------------------------------------------|:----------------|:--------|-----------:|:----------|
| 0 | 7306010 | 06-01-2020 | Lenovo V145 15.6" FHD Laptop AMD A9-9425, 8GB RAM, 256GB SSD, Black | IntelliWorld | £444.99 | 1 | Delivered |
| 1 | 7224010 | 24-12-2019 | Samsung Galaxy M30s SIM Free Smartphone | DigiTec Limited | £249.00 | 4 | Delivered |
| 2 | 7224010 | 24-12-2019 | Nikon Digital DSLR Camera Body (Black) | Orinoco UK | £721.00 | 1 | Delivered |
| 3 | 7130010 | 30-11-2019 | Nintendo Switch | Orinoco UK | £329.99 | 6 | Delivered |
| 4 | 7102000 | 02-11-2019 | Huawei Y6 32GB 6.09 inch Smartphone | DigiTec Limited | £107.50 | 2 | Delivered |
| 5 | 6731010 | 31-07-2019 | Numskull Multi-Format Pro Steering Wheel | Orinoco UK | £69.99 | 3 | Delivered |
| 6 | 7304010 | 04-01-2019 | Canon DSLR Camera (Black) | Orinoco UK | £249.00 | 12 | Delivered |
推荐阅读
- spring - 如何在休眠条件动态查询中使用多个表?
- css - 将 img 移到中间
- c++14 - 我找不到除以零的位置,因为我将“inf”作为 I/P 的 O/P
- delphi - Graphics32 和 VCL 样式
- android - 如何将逻辑放入对话框中?还是做动态对话?
- r - R中的字符排序
- netlogo - 将 NetLogo GUI 模型转换为在集群上运行无头模式所需的所有步骤是什么?
- amazon-web-services - 配置 dynamoDb 流以仅在删除时调用 lambda 函数
- excel-formula - 如何在 Excel 中以特定间隔实现公式
- android - 约束布局不能在 API 21、22 上正确运行吗?