2

I have encountered an error when writing my program and would like some help with it. The program has to replace a specific segment of the line with a calculated value. The segment in which I am talking about is the third index in a line, formatted like this:

Product01, 12346554, 15, 6

I am having the program calculate the value in which I want to replace with the final value but no matter how much I seem to try, my code frequently gives errors. For an example, I have used seek to try and move the cursor to allow myself to edit this value, however with the following code:

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

However, when I run it this is the error that I get:

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

I was wondering if anyone could help, and/or provide a different answer to my problem? Thanks in advance.

Elyscard
  • 21
  • 1
  • 2
    what you mean by `L = (line)` ? `line` is string then `L` will be string too. – ᴀʀᴍᴀɴ Feb 14 '17 at 14:48
  • 1
    `searchfile.seek(27,0)` instead of `L.seek(27, 0)`. – Abdou Feb 14 '17 at 14:48
  • @Abdou we're searching the line and not the full file. – Elyscard Feb 14 '17 at 14:49
  • @Arman Was left over code from a previous int() test. – Elyscard Feb 14 '17 at 14:50
  • 2
    If you're reading and writing comma-delimited data, you should really just use the `csv` module instead of messing around with character positions. – tzaman Feb 14 '17 at 14:53
  • 3
    @Elyscard, `line` is a string and does not have a `seek` or `write` method. If you wish to edit the `line` and put it back then I recommend you consume the whole content, iterate through it, make your changes and put the whole thing back. – Abdou Feb 14 '17 at 14:53
  • @tzaman Unfortunately, due to the circumstances of the task we can't exactly use CSVs. Would love to though. – Elyscard Feb 14 '17 at 14:59
  • Possible duplicate of [Is it possible to modify lines in a file in-place?](http://stackoverflow.com/questions/5453267/is-it-possible-to-modify-lines-in-a-file-in-place) – glibdud Feb 14 '17 at 15:11
  • `Prod_Code` is a string and will never `== 8` – Paul H Feb 14 '17 at 15:42

0 Answers0