【问题标题】:How can I convert the information in the list into a table?如何将列表中的信息转换为表格?
【发布时间】:2022-01-21 09:25:03
【问题描述】:

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

    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中的lists中的物品打印到表格中。

购物清单示例:[['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

【问题讨论】:

  • 请在您的问题中添加购物清单
  • @PSR 我编辑了消息。
  • documnet 中的预期输出是什么样的? .你能不能告诉我你所显示的列表是怎样的。添加文本文档的屏幕截图,其中包含您期望的输出结果
  • @PSR 我通过编辑消息添加了我想要的输出。对不起。

标签: python list pyqt5


【解决方案1】:

您可以使用 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 |
+----+-----------+----------------+---------+--------------+---------+-----------+------------+

【讨论】:

  • 谢谢,我们能否也添加一个破折号来显示数据彼此分开?
  • 我编辑了答案!
  • 我遇到了一个错误。我在留言中说明了。
【解决方案2】:

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

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 返回 2 个元素。正确的语法应该是:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    相关资源
    最近更新 更多