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 |
--------- -------------- ------- ------------ ------- --------- ------------
CodePudding user response:
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 |
---- ----------- ---------------- --------- -------------- --------- ----------- ------------
CodePudding user response:
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 ?
