Python: reading values ​​into an empty array and then back to a file

I would like to read numbers from a txt file (highscore.txt) in python and assign those values ​​to an empty array (records).

Then I would like to add another value to the array and then overwrite the file with the new values ​​in the array.

I put together the program, but it doesn't give me the desired result. See what's wrong with him ...

Reading from a file:

highscores = []
#Read values from file and put them into array
file = open('highscore.txt', 'r') #read from file
file.readline() #read heading line
for line in file:
    highscores.append(file.readline())
file.close() #close file 

      

Add value and overwrite file:

highscores.append(wins)
# Print sorted highscores print to file
file = open('highscore.txt', 'w') #write to file
file.write('Highscores (number of wins out of 10 games):\n') #write heading line
for line in highscores:
    file.write(str(line))
file.close() #close file

      

It should work in such a way that I can (once it works) sort the array with the added value, before overwriting the file again ...

I expect to read from a file:

Highscores (number of wins out of 10 games):
8
6
5
5
3
1
0
0

      

Read this value into an array Then add (let's say) 4 to the array Then overwrite the file with new values

In this case, we can expect the result to be:

Highscores (number of wins out of 10):
8
6
5
5
3
1
0
0
4

      

Hope you can find out what's wrong there ...

Edit: Thanks to EvenListe's answer I could find a solution, here is the relevant code I used to get my program to work perfectly (includes the added array sorted in descending order after adding)

from __future__ import print_function

highscores = []
with open("highscore.txt", "r") as f:
    f.readline() # Reads header
    for line in f:
        highscores.append(line.strip())

highscores.append(wins)
highscores = sorted(highscores, key=int, reverse=True)
# Print sorted highscores print to file
with open("highscore.txt", "w") as f:
  for val in highscores:
    print(val, file=f)

      

If you want to check which lines in the file you can use (I used it to add the array and after adding the array, it really helps to figure out what's wrong if you don't have to constantly open the file):

print('Highscores (number of wins out of 10 games):')
for lines in highscores:
    print(lines)

      

+3


source to share


2 answers


It's hard to tell what's wrong without seeing the expected result and the actual result, but I'm guessing what you need to strip \n

from the lines you read:



from __future__ import print_function
highscores = []
with open("highscore.txt", "r") as f:
  for line in f:
    highscores.append(line.strip())

highscores.append(wins)
with open("highscore.txt", "w") as f:
  for val in highscores:
    print(val, file=f)

      

+1


source


From what I can tell, one obvious problem with your code is yours

for line in infile:
    highscores.append(infile.readline())

      

which skips every other line. You must have



for line in infile:
    highscores.append(line)

      

or simpler:

highscores=infile.readlines()
highscores=highscores[1:]  #Remove header

      

+2


source







All Articles