如何将列表中的信息转换成table?

How can I convert the information in the list into a table?

有一张购物清单。此列表还包括产品列表。购物清单列出了所有购买的物品。商品列表是显示每个购买商品信息的列表。

    resetFile = open("printMachine.txt", "w")
    resetFile.write("")
    shoppingList = []
    for row in enumerate(range(self.arayuz.shop_shoppingListTableWidget.rowCount())):
        product_info = []
        for col in enumerate(range(self.arayuz.shop_shoppingListTableWidget.columnCount())):
            alt_liste.append(self.shop_shoppingListTableWidget.item(row[0], col[0]).text())
        shoppingList.append(product_info)

我想将此列表中的信息以表格形式打印到文件中。我该怎么做?

更简单地说,我想将 shoppingList 中列表中的项目打印到 table。

购物清单示例:[['12', 'Pencil', 'Yok', 'Yok', '1', '2', '21/01/2022'], ['13', 'Bag', 'Yok', 'Yok', '1', '25', '21/01/2022'], ['14', 'Book', 'Yok', 'Yok', '2', '5', '21/01/2022']]

我希望将其写入文件:

+---------+--------------+-------+------------+-------+---------+------------+
| Barcode | Product Name | Brand | Piece/Gram | Price | Payable |    Date    |
+---------+--------------+-------+------------+-------+---------+------------+
|   12    |    Pencil    | None  |    None    |   1   |    2    | 21/01/2022 |
+---------+--------------+-------+------------+-------+---------+------------+
|   13    |     Bag      | None  |    None    |   1   |   25    | 21/01/2022 |
+---------+--------------+-------+------------+-------+---------+------------+
|   14    |     Book     | None  |    None    |   2   |    5    | 21/01/2022 |
+---------+--------------+-------+------------+-------+---------+------------+

我试过这段代码:

        df = pd.DataFrame(liste)
        df.columns = ["Barkod", "Ürün Adı", "Ürün Markası", "Ürün Kategorisi" "Ürün Adeti/Gramı", "Ürün Fiyatı", "Tarih"]
        writeFile.write(tabulate(df, headers='keys', tablefmt='psql'))
        writeFile.write("\n\n\nTotal cost::" + self.arayuz.shop_totalCostTextBox.text() + " TL")
        writeFile.write("\n\n--------------------------------\n\nBu kağıt, resmi bir belge değildir; sadece bilgilendirme amaçlıdır.")

错误:

    tabulate(df, df.columns, tablefmt='psql')
TypeError: 'module' object is not callable

不清楚你想要什么。 您应该尝试类似的操作:

resetFile.write("ROW  | COL PRODUCT INFO \n")
for i, product_info in enumerate(shoppingList):
    for info in product_info:
        resetFile.write(f"{i}   | {info} \n")

顺便说一句,我认为你的语法是错误的,因为 enumerate returns 2 个元素。正确的语法应该是:

for i, row in enumerate(range(self.arayuz.shop_shoppingListTableWidget.rowCount())):

加上 range 已经 return 一个迭代数,为什么要使用枚举?

您可以使用 pandas 数据框:

import pandas as pd
ls = [['12', 'Pencil', 'Yok', 'Yok', '1', '2', '21/01/2022'], ['12', 'Book', 'Yok', 'Yok', '2', '5', '21/01/2022'], ['12', 'Bag', 'Yok', 'Yok', '1', '25', '21/01/2022']]
df = pd.DataFrame(ls)
print(df)

结果

   0   1       2    3    4   5           6
0  12  Pencil  Yok  Yok  1   2  21/01/2022
1  12    Book  Yok  Yok  2   5  21/01/2022
2  12     Bag  Yok  Yok  1  25  21/01/2022

您可以更改列名,例如:

df.columns = ['Barcode' , 'Product Name' , 'Brand' , 'Piece/Gram' , 'Price' , 'Payable' ,    'Date' ]
print(df)

结果

    Barcode Product Name Brand Piece/Gram Price Payable  Date
0      12       Pencil   Yok        Yok     1       2  21/01/2022
1      12         Book   Yok        Yok     2       5  21/01/2022
2      12          Bag   Yok        Yok     1      25  21/01/2022

编辑漂亮的印刷品 (see this answer)

from tabulate import tabulate
import pandas as pd
print(tabulate(df, headers='keys', tablefmt='psql'))

结果

+----+-----------+----------------+---------+--------------+---------+-----------+------------+
|    |   Barcode | Product Name   | Brand   | Piece/Gram   |   Price |   Payable | Date       |
|----+-----------+----------------+---------+--------------+---------+-----------+------------|
|  0 |        12 | Pencil         | Yok     | Yok          |       1 |         2 | 21/01/2022 |
|  1 |        12 | Book           | Yok     | Yok          |       2 |         5 | 21/01/2022 |
|  2 |        12 | Bag            | Yok     | Yok          |       1 |        25 | 21/01/2022 |
+----+-----------+----------------+---------+--------------+---------+-----------+------------+