簡體   English   中英

Python-搜索文本文件

[英]Python - Searching text file

我正在制作一個程序,該程序需要我遍歷文本文件並完成總和,任何低於要求值的值都需要添加到發票文件中。 但是,我創建的代碼只寫一種產品,而不是所有需要補貨的產品。

這是主要代碼:

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。 任何幫助將不勝感激

您需要更改打開invoice.txt的模式。對於功能,您需要將其從r+更改為a+ 它是寫第二十九張發票,然后刪除它,然后寫wispa。

以下代碼對我有用。

我已將代碼中打開發票文件的位置移到了主程序中,因此您可以保持打開模式“ 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM