【发布时间】:2022-12-04 05:08:53
【问题描述】:
我试图将每一行存储在列表的不同元素中。文本文件如下...
244
Large Cake Pan
7
19.99
576
Assorted Sprinkles
3
12.89
212
Deluxe Icing Set
6
37.97
827
Yellow Cake Mix
3
1.99
194
Cupcake Display Board
2
27.99
285
Bakery Boxes
7
8.59
736
Mixer
5
136.94
我试图让 244、576 等在 ID 中。以及名称中的“大蛋糕盘”、“什锦糖屑”等。你明白了,但是它把所有的东西都存储在 ID 中,我不知道如何让它把信息存储在它对应的元素中。
到目前为止,这是我的代码:
import Inventory
def process_inventory(filename, inventory_dict):
inventory_dict = {}
inventory_file = open(filename, "r")
for line in inventory_file:
line = line.split('\n')
ID = line[0]
Name = line[1]
Quantity = line[2]
Price = line[3]
my_inventory = Inventory.Inventory(ID, Name, Quantity, Price)
inventory_dict[ID] = my_inventory
inventory_file.close()
return inventory_dict
def main():
inventory1={}
process_inventory("Inventory.txt", inventory1)
【问题讨论】:
-
question 的答案有帮助吗?
标签: python list file dictionary