0

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?

3 Answers 3

1

I recommend this approach:

with open('Premier_League.txt', 'r+') as f:
    data = [int(line.strip()) for line in f.readlines()] # [1, 2, 3]
    f.seek(0)

    i = int(input("Please put in your result! \n"))

    data[i] += 1  # e.g. if i = 1, data now [1, 3, 3]

    for line in data:
        f.write(str(line) + "\n")

    f.truncate()
Sign up to request clarification or add additional context in comments.

4 Comments

data = [int(line.strip()) for line in f.readlines()] # [1, 2, 3] ValueError: invalid literal for int() with base 10: ''
@PythonGirl make sure you reset your Premier_League.txt file... if it contains '2\\n' as in your question, you'll receive that error.
Thank so you freaking much! You have saved my day!
@PythonGirl glad I could help!
1

f readlines() read all content in a list as a string,if you want write back those contents as int

data=[]
with open ("Premier_League.txt", "r+") as f:
        i= int(input("Please put in your result! \n"))
        data = f.readlines()

with open ("Premier_League.txt", "w") as f:            
        for j in data:
            f.write(str(int(j)+1))
            #or do this to make it more clean,these lines are comments
            #j=int(j)+1
            #f.write(str(j))



         # <-- cut any leftovers from the old version
print(data)

Note that,once you open a file,if you don't close it,your written contents can be lost,whatever you want to do with data,your have to do it in the second writing method .Also notice the change from r to w in with open ("Premier_League.txt", "w") for writing

5 Comments

It only tells me that "data is not defined"
Do you see on the top that data is declared as data=[],right on the top of my codes
You don't need to close the file when using a context manager. Nor is it necessary to open the file twice.
@flevinkelming,true,i forgot
tells me: TypeError: write() argument must be str, not int
1

Following my solution:

with open ("Premier_League.txt", "r+") as f:
        i= int(input("Please put in your result! \n"))
        # here strip wrong chars from input 
        data = f.readlines()
        print(data)
        # here added the str(..) conversion
        data[i] = str(int(data[i].strip())+1) + '\n'
        f.seek(0) # <-- rewind to the beginning
        # the str(data) is wrong, data is a list!
        f.writelines(data)
        # I don't think this is necessary
        # f.truncate() # <-- cut any leftovers from the old version
        print(data)
        # i think this is not necessary
        # data[i] = str(data)

2 Comments

Hi I tried your solution (and put i=0) and this happened, file contained ['0', '1', '2', '3'], first run gave me ['1', '1', '2', '3'] which is exactly what I wanted!, the second run gave me ['22344', '4', ''] which is clearly wrong...
Oh I missed that writelines does not automatically add the newline char at the end of each element. I updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.