有一個購物清單。此串列還包括產品串列。購物清單列出了所有購買的物品。產品串列是顯示每個購買的產品資訊的串列。
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 中的串列中的專案列印到表格中。
購物清單示例: [['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
uj5u.com熱心網友回復:
您可以使用熊貓資料框:
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
編輯漂亮的印刷品(見這個答案)
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 |
---- ----------- ---------------- --------- -------------- --------- ----------- ------------
uj5u.com熱心網友回復:
不清楚你想要什么。您應該嘗試類似的方法:
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已經回傳一個迭代數,為什么要使用 enumerate ?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/419224.html
標籤:
上一篇:在具有相同鍵的物件中聚合串列
下一篇:Java-如何迭代哈希圖串列?
