Python:将值读入空数组,然后返回文件
Python: Read values into empty array and then back into file
我想从 python 中的 txt 文件 (highscore.txt) 中读取数字并将这些值分配给一个空数组(高分)。
然后我想在数组中添加另一个值,然后用数组中的新值覆盖文件。
我编写了一个程序,但它没有给我想要的输出。请大家看看有什么问题...
从文件读取:
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
添加值并覆盖文件:
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
它需要以这样一种方式工作,即我可以(一旦它起作用)在再次覆盖文件之前用添加的值对数组进行排序...
我希望从文件中读取:
Highscores (number of wins out of 10 games):
8
6
5
5
3
1
0
0
将这些值读入数组
然后将(比方说)4 添加到数组中
然后用新值覆盖文件
在这种情况下,我们可以预期输出为:
Highscores (number of wins out of 10):
8
6
5
5
3
1
0
0
4
希望你能找出问题所在...
编辑:感谢 EvenListe 的回答,我找到了一个解决方案,这是我用来让我的程序完美运行的相关代码(包括添加的数组在添加后按降序排序)
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)
如果你想测试文件中的行是什么,你可以使用它(我在添加数组之前和添加数组之后使用它,它确实有助于找出问题所在而无需不断打开文件):
print('Highscores (number of wins out of 10 games):')
for lines in highscores:
print(lines)
如果没有看到预期结果与实际结果,很难说出问题所在,但我猜你需要从你阅读的行中删除 \n
:
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)
据我所知,您的代码的一个明显问题是您的
for line in infile:
highscores.append(infile.readline())
每隔一行跳过一次。你应该
for line in infile:
highscores.append(line)
或更简单:
highscores=infile.readlines()
highscores=highscores[1:] #Remove header
我想从 python 中的 txt 文件 (highscore.txt) 中读取数字并将这些值分配给一个空数组(高分)。
然后我想在数组中添加另一个值,然后用数组中的新值覆盖文件。
我编写了一个程序,但它没有给我想要的输出。请大家看看有什么问题...
从文件读取:
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
添加值并覆盖文件:
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
它需要以这样一种方式工作,即我可以(一旦它起作用)在再次覆盖文件之前用添加的值对数组进行排序...
我希望从文件中读取:
Highscores (number of wins out of 10 games):
8
6
5
5
3
1
0
0
将这些值读入数组 然后将(比方说)4 添加到数组中 然后用新值覆盖文件
在这种情况下,我们可以预期输出为:
Highscores (number of wins out of 10):
8
6
5
5
3
1
0
0
4
希望你能找出问题所在...
编辑:感谢 EvenListe 的回答,我找到了一个解决方案,这是我用来让我的程序完美运行的相关代码(包括添加的数组在添加后按降序排序)
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)
如果你想测试文件中的行是什么,你可以使用它(我在添加数组之前和添加数组之后使用它,它确实有助于找出问题所在而无需不断打开文件):
print('Highscores (number of wins out of 10 games):')
for lines in highscores:
print(lines)
如果没有看到预期结果与实际结果,很难说出问题所在,但我猜你需要从你阅读的行中删除 \n
:
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)
据我所知,您的代码的一个明显问题是您的
for line in infile:
highscores.append(infile.readline())
每隔一行跳过一次。你应该
for line in infile:
highscores.append(line)
或更简单:
highscores=infile.readlines()
highscores=highscores[1:] #Remove header