我想开发该程序以便能够从文本文件中检索和显示信息,所以我想知道如何指出它。
我想通过用户输入某个产品代码和通过文本文件搜索它的程序来检索信息。 另外,我希望用户能够输入一定数量的产品,并且程序能够使用文本文件中的单个价格计算总成本。 我需要它来显示相关信息,如项目名称和价格。 我也想要它识别任何未找到的产品。 输出应该几乎就像一张收据。
我还没有任何代码,因为我不知道从哪里开始,但是iIhave已经创建了包含所有数据的文本文件。
这是我的文本文件:
GTIN: 34512340 plain brackets Quantity: 4 Individual Price: 0.50 GTIN: 98981236 product not found GTIN: 56756777 100 mm bolts Quantity: 32 Individual Price: 0.20 GTIN: 90673412 L-shaped brackets Quantity: 6 Individual Price: 1.20 GTIN: 13245627 100 mm nuts Quantity: 32 Individual Peice: 0.20I would like to develop the program to be able to retrieve and display information from a text file so I would like to know how to point it.
I would like to retrieve the information by the user inputting a certain product code and the program searching through the text file for it. In addition, I would like the user to be able to input a certain quantity of a product and the program be able to calculate the total cost using the individual price from the text file. I need it to display the related information such as an item name and price. I would also like it to identify any products not found. The output should be almost like a receipt.
I do not have any code as of yet as I do not know where to start but iIhave made the text file including all of its data.
This is my text file:
GTIN: 34512340 plain brackets Quantity: 4 Individual Price: 0.50 GTIN: 98981236 product not found GTIN: 56756777 100 mm bolts Quantity: 32 Individual Price: 0.20 GTIN: 90673412 L-shaped brackets Quantity: 6 Individual Price: 1.20 GTIN: 13245627 100 mm nuts Quantity: 32 Individual Peice: 0.20最满意答案
你可以使用csv模块,你的文本文件必须提供一些规则, (但是你不必使用它可以是空格,冒号(:)等)字符beetwen列:
import csv with open('c.txt', 'r') as cfile: content = csv.reader(cfile, delimiter=',') for row in content: print(row)# c.txt GTIN, Quantity, Individual Price 34512340 plain brackets, 4, 0.50 34512340 product not found 56756777 100 mm bolts, 32, 0.20 # output ['GTIN', ' Quantity', ' Individual Price'] ['34512340 plain brackets', ' 4', ' 0.50'] ['34512340 product not found'] ['56756777 100 mm bolts', ' 32', ' 0.20']
python csv
You can use csv module, there are some rules your text file must provide like ,(but ofcourse you dont have to use this it can be a space, colon(:) etc.) character beetwen columns :
import csv with open('c.txt', 'r') as cfile: content = csv.reader(cfile, delimiter=',') for row in content: print(row)# c.txt GTIN, Quantity, Individual Price 34512340 plain brackets, 4, 0.50 34512340 product not found 56756777 100 mm bolts, 32, 0.20 # output ['GTIN', ' Quantity', ' Individual Price'] ['34512340 plain brackets', ' 4', ' 0.50'] ['34512340 product not found'] ['56756777 100 mm bolts', ' 32', ' 0.20']
python csv
更多推荐
发布评论