Python - 写入一行中的特定点

时间:2017-02-14 14:46:57

标签: python

我在编写程序时遇到错误,并希望得到一些帮助。程序必须用计算值替换该行的特定段。我正在讨论的部分是一行中的第三个索引,格式如下:

Product01, 12346554, 15, 6

我正在让程序计算我想用最终值替换的值,但不管我多少尝试,我的代码经常会出错。例如,我使用seek尝试移动光标以允许自己编辑此值,但是使用以下代码:

Total = 0
receipt = open("Receipt.txt", "w")
while True:
    try:
        Prod_Code = input("Enter a code or Done to get your final receipt: ")
        if len(Prod_Code) == 8:
            int(Prod_Code)
            with open("Data Base.txt", "r+") as searchfile:
                for line in searchfile:
                    if Prod_Code in line:
                        print(line)
                        Quantity = input("What quantity of this product do you want? ")
                        Total += float(line.split(",")[2]) * int(Quantity)
                        print(Quantity)
                        print(Total)
                        receipt.write(line)
                        print(line.split(",")[3])
                        W = int(line.split(",")[3]) - int(Quantity)
                        print(W)
                        L = (line)
                        L.seek(27, 0)
                        L.write(str(W))

        elif Prod_Code == "Done":
            receipt.close()
            with open("Receipt.txt", "r") as datafile:
                for item in datafile:
                    print(item.split(",")[1])
                    print(item.split(",")[2])

            print("Your total cost is £", Total)    
            input("Press Enter to exit:")
            exit()
    else:
        print("Incorrect length, try again")
except ValueError: 
    print("You must enter an integer")

然而,当我运行它时,这是我得到的错误:

AttributeError: 'str' object has no attribute 'seek'.

我想知道是否有人可以提供帮助,和/或为我的问题提供不同的答案?提前谢谢。

0 个答案:

没有答案