[英]Python - Searching text file
I am making a programme that requires me to iterate through a text file and complete a sum, any values that are below the required value need to be added to an invoice file. 我正在制作一个程序,该程序需要我遍历文本文件并完成总和,任何低于要求值的值都需要添加到发票文件中。 However the code that I have created only writes one product rather then every one that is required to be restocked.
但是,我创建的代码只写一种产品,而不是所有需要补货的产品。
Here is the main code: 这是主要代码:
def createRestockFile(productName,minimumStockLevel,currentStock, amountNeeded,costToUs):
with open("invoice.txt", 'r+') as f:
f.write("#Product Name\tMinimum Stock Level\tCurrent Stock Level\tAmount Needed\tCost To Re-Order \n")
f.write("%s\t%s\t%s\t%s\t%s" % (productName,minimumStockLevel,currentStock,amountNeeded,costToUs))
def checkStock():
with open("stock.txt",'r+') as f:
for line in f:
if int(line.split()[2]) < int(line.split()[5]):
amountNeeded = int(line.split()[5]) - int(line.split()[2])
total = '£{:,.2f}'.format(float(line.split()[3])*amountNeeded)
createRestockFile(line.split()[1],line.split()[5],line.split()[2],amountNeeded,total)
print(line.split())
def startProgramme():
yesInput = ["yes", "yes please", "y"]
noInput = ["no","nope","n"]
print("Welcome to Sean's Stock re-order programme")
choice = input("Would you like to check which products need re-ordering ")
if choice in yesInput:
checkStock()
elif choice in noInput:
import time
print("Thank you for using Sean's re-order programme")
print("Ending Programme")
time.sleep(0.6)
exit()
startProgramme()
Here is the invoice file: 这是发票文件:
#Product Name Minimum Stock Level Current Stock Level Amount Needed Cost To Re-Order
Wispa 16 6 10 £3.4003.40
Here is the stock file: 这是库存文件:
45678948 Twix 12 0.42 0.65 25 50
12345670 Wispa 6 0.34 0.85 16 40
26073125 Crunchie 37 0.37 0.69 8 43
24785122 Flake 47 0.24 0.65 10 35
45678914 Snickers 42 0.46 0.75 8 32
78945617 Maltesers 78 0.32 0.56 12 65
85146945 Galaxy 57 0.32 0.76 9 54
With the given values in the stock file, the programme should add both the twix and wispa to the invoice file, however only the wispa is added. 在库存文件中具有给定值的情况下,程序应将twix和wispa都添加到发票文件中,但是仅添加wispa。 Any help would be greatly appreciated
任何帮助将不胜感激
You need to change the mode you open the invoice.txt in. For the function, you need to change it from r+
to a+
; 您需要更改打开invoice.txt的模式。对于功能,您需要将其从
r+
更改为a+
; it is writing the twix invoice then deleting it and then writing wispa. 它是写第二十九张发票,然后删除它,然后写wispa。
The following code works for me. 以下代码对我有用。
I have moved the position in the code where you open the invoice file into the main program so you can keep your opening mode "w+". 我已将代码中打开发票文件的位置移到了主程序中,因此您可以保持打开模式“ w +”。 Note also that I wrote code so that you split the input line only once (saves time and makes the code shorter)
还要注意,我编写了代码,以便您仅将输入行分割一次(节省时间,并使代码更短)
def createRestockFile(productName,minimumStockLevel,currentStock, amountNeeded,costToUs, f):
f.write("%s\t%s\t%s\t%s\t%s" % (productName,minimumStockLevel,currentStock,amountNeeded,costToUs) + "\n")
def checkStock(invoiceFile):
with open("stock.txt",'r+') as f:
for line in f:
splits = line.split()
if int(splits[2]) < int(splits[5]):
amountNeeded = int(splits[5]) - int(splits[2])
total = '£{:,.2f}'.format(float(splits[3])*amountNeeded)
createRestockFile(splits[1],splits[5],splits[2],amountNeeded,total, invoiceFile)
print(splits)
def startProgramme():
yesInput = ["yes", "yes please", "y"]
noInput = ["no","nope","n"]
print("Welcome to Sean's Stock re-order programme")
choice = input("Would you like to check which products need re-ordering ")
if choice in yesInput:
invoice_f = open("invoice.txt", 'w+')
invoice_f.write("#Product Name\tMinimum Stock Level\tCurrent Stock Level\tAmount Needed\tCost To Re-Order \n")
checkStock(invoice_f)
invoice_f.close()
elif choice in noInput:
import time
print("Thank you for using Sean's re-order programme")
print("Ending Programme")
time.sleep(0.6)
exit()
startProgramme()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.