首页 > 解决方案 > 在 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

我试图让它更像这样(点击)

任何帮助将不胜感激

标签: pythonlisttuples

解决方案


  1. 避免重复相同的 ID 和日期

    • 您需要跟踪以前的 ID,并检查它是否print基于该ID 进行更改
  2. 获得良好的列格式

    • 您需要在格式字符串中指定一些大小,例如{: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 |

推荐阅读