Python搜索文本fi

2021-10-17 14:25:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个程序,要求我迭代通过一个文本文件,并完成一个总和,任何低于所需值的值需要添加到一个发票文件。然而,我创建的代码只编写了一个产品,而不是每个需要重新进货的产品。你知道吗

主要代码如下:

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()

这是发票文件:

#Product Name   Minimum Stock Level Current Stock Level Amount Needed   Cost To Re-Order 
Wispa   16  6   10  £3.4003.40

这是股票文件:

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  

对于库存文件中的给定值,程序应该将twix和wispa添加到发票文件中,但是只添加wispa。任何帮助都将不胜感激

2条回答
网友
1楼 ·

你需要改变你打开电脑的方式发票.txt在。对于函数,您需要将其从r+更改为a+;它正在写入twix发票,然后删除它,然后写入wispa。你知道吗

网友
2楼 ·

下面的代码适用于我。你知道吗

我已经将代码中打开发票文件的位置移到主程序中,这样就可以保持打开模式为“w+”。另外请注意,我编写的代码使您只拆分一次输入行(节省时间并缩短代码)

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()

相关问题