Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Read values into empty array and then back into file

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

Then I'd like to add another value into the array and then overwrite the file with the new values in the array.

I've put together a program but it doesn't give me the desired output. Please see what's wrong with it...

Read from 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 needs work in such a way that I can (once this is working) 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 those value into the array Then to add (let's say) 4 to the array Then overwrite the file with the new values

In this case we can expect the output 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's the relevant code that I've used to get my program working perfectly (includes the array that gets added being sorted in descending order after being added)

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 test out what the lines in the file are you can use this (I used it for before adding the array and after adding the array, it really helps finding out what's wrong without you having to constantly open the file):

print('Highscores (number of wins out of 10 games):')
for lines in highscores:
    print(lines)
like image 601
Barry Michael Doyle Avatar asked May 17 '26 16:05

Barry Michael Doyle


2 Answers

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

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

which skips every other line. You should have

for line in infile:
    highscores.append(line)

or easier:

highscores=infile.readlines()
highscores=highscores[1:]  #Remove header
like image 101
Christoph Avatar answered May 19 '26 06:05

Christoph


Hard to tell what's wrong without seeing the expected result vs actual result, but my guess is you need to strip the \n from the lines your 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)
like image 45
EvenLisle Avatar answered May 19 '26 04:05

EvenLisle