I need help with a problem concerning the code below.
with open ("Premier_League.txt", "r+") as f:
i= int(input("Please put in your result! \n"))
data = f.readlines()
print(data)
data[i] = int(data[i])+1
f.seek(0) # <-- rewind to the beginning
f.writelines(str(data))
f.truncate() # <-- cut any leftovers from the old version
print(data)
data[i] = str(data)
For example if the file Premier_League.txt contains:
1
2
3
and as I run the program and choose i as 0
that gives me:
[2, '2\n', '3']
and saves it to the already existing file (and deletes the old content) But after that I cannot run the program again and it gives me this:
ValueError: invalid literal for int() with base 10: "[2, '2\\n', '3']"
My question is: How do I make the new file content suitable to go into the program again?