0

There is a shopping list. This list also includes a product list. The shopping list lists all purchased items. The product list is the list that shows the information of each purchased product.

    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)

I want to print the information in this list to the file in tabular form. How can I do it?

To put it more simply, I want to print the items in the lists in the shoppingList to the table.

Example shoppingList: [['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']]

I want it to be written to the file as:

+---------+--------------+-------+------------+-------+---------+------------+
| 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 |
+---------+--------------+-------+------------+-------+---------+------------+

I tried this code:

        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.")

Error:

    tabulate(df, df.columns, tablefmt='psql')
TypeError: 'module' object is not callable
Emir
  • 7
  • 3
  • Add the shopping list in your question please – PSR Jan 21 '22 at 09:42
  • @PSR I edited message. – Emir Jan 21 '22 at 09:58
  • what does the expected output look like in the documnet? . Could you jsut show me how it would be for the list you have shown. Add a screenshot of the text document with how you expect the output to be – PSR Jan 21 '22 at 10:00
  • @PSR I added exactly the output I wanted by editing the message. Sorry. – Emir Jan 21 '22 at 10:14

2 Answers2

0

It is not clear about what you want. You should try something similar to this :

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")

By the way, i think your syntax is wrong, as enumerate returns 2 elements. The correct syntax should be :

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

Plus the range already return an iterative number, why using enumerate ?

ELCaptain
  • 66
  • 4
0

You can use pandas dataframe:

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)

Result

   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

You can change column names like:

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

Result

    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

Edit for pretty print (see this answer)

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

Result

+----+-----------+----------------+---------+--------------+---------+-----------+------------+
|    |   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 |
+----+-----------+----------------+---------+--------------+---------+-----------+------------+
Inputvector
  • 1,061
  • 10
  • 22