重写我的分数文本文件以确保它只有最后 4 个分数 (python)
Rewriting my scores text file to make sure it only has the Last 4 scores (python)
以前,在 SO 用户的帮助下,我能够找到如何在最大长度 属性 的 Python 文档中存储最多 4 个键字典里面。
现在,我想更进一步。下面是一个文本文件,其中包含我的参与者 - 戴夫、杰克和亚当的所有 最近 乐谱。
Jack:10
Dave:20
Adam:30
Jack:40
Adam:50
Dave:60
Jack:70
Dave:80
Jack:90
Jack:100
Dave:110
Dave:120
Adam:130
Adam:140
Adam:150
现在,这是我的代码,可以让我看到 Python 中的最后 4 个分数:
import collections
from collections import defaultdict
scores_guessed = collections.defaultdict(lambda: collections.deque(maxlen=4))
with open('Guess Scores.txt') as f:
for line in f:
name,val = line.split(":")
scores_guessed[name].appendleft(int(val))
for k in sorted(scores_guessed):
print("\n"+k," ".join(map(str,scores_guessed[k])))
writer = open('Guess Scores.txt', 'wb')
for key, value in scores_guessed.items():
writer.writerow([key,':',value])
很明显,它将打印出字典的以下结果:
Adam 150 140 130 50
Dave 120 110 80 60
Jack 100 90 70 40
但是,我希望文件按字母顺序向我读出最后四个结果:
Adam:150
Adam:140
Adam:130
Adam:50
Dave:120
Dave:110
Dave:80
Dave:60
Jack:100
Jack:90
Jack:70
Jack:40
我认为这段代码行得通:
for key, value in scores_guessed.items():
writer.writerow([key,':',value])
然而这个 returns 我的结果是:
AttributeError: '_io.BufferedWriter' object has no attribute 'writerow'
例如,如果亚当得了 200 分,我希望 scores_guessed 重写为:
Adam:200
Adam:150
Adam:140
Adam:130
出了什么问题?
UPDATE - 为了回应下面的第一个答案,我将最后一个代码块编辑为:
for key, value in scores_guessed.items():
writer.write("{}:{}\n".format(key,value))
然而它给了我这个信息:
writer.write("{}:{}\n".format(key,value))
类型错误:'str' 不支持缓冲区接口
发生了什么事?
你使用的writer.writerow
语法对应the csv module,用法如下:
import csv
with open('some_file.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['Spam'] * 5 + ['Baked Beans'])
writer.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
在我看来,您使用了一些错误的引用来执行此操作,因为您没有在上面的代码中使用 csv
模块。
因此,更改您的以下代码
for key, value in scores_guessed.items():
writer.writerow([key,':',value])
此代码:
for key, value in scores_guessed.items():
output = "%s:%s\n" % (key,value)
writer.write(output)
编辑
您正在以二进制模式打开文件,而不是使用
以文本模式打开文件
writer = open('Guess Scores.txt', 'wt')
for key, value in scores_guessed.items():
output = "{}:{}\n".format(key,value)
writer.write(output)
writer.close()
编辑 2
既然你用的是双端队列,那就用:
writer = open('Guess Scores.txt', 'wt')
for key, value in scores_guessed.items():
output = "{}:{}\n".format(key,','.join(map(str, scores_guessed[key])))
writer.write(output)
writer.close()
或使用:
with open("output.txt", "wt") as f:
for key in scores_guessed:
f.write("{} {}".format(key, ','.join(map(str, scores_guessed[key]))))
以前,在 SO 用户的帮助下,我能够找到如何在最大长度 属性 的 Python 文档中存储最多 4 个键字典里面。
现在,我想更进一步。下面是一个文本文件,其中包含我的参与者 - 戴夫、杰克和亚当的所有 最近 乐谱。
Jack:10
Dave:20
Adam:30
Jack:40
Adam:50
Dave:60
Jack:70
Dave:80
Jack:90
Jack:100
Dave:110
Dave:120
Adam:130
Adam:140
Adam:150
现在,这是我的代码,可以让我看到 Python 中的最后 4 个分数:
import collections
from collections import defaultdict
scores_guessed = collections.defaultdict(lambda: collections.deque(maxlen=4))
with open('Guess Scores.txt') as f:
for line in f:
name,val = line.split(":")
scores_guessed[name].appendleft(int(val))
for k in sorted(scores_guessed):
print("\n"+k," ".join(map(str,scores_guessed[k])))
writer = open('Guess Scores.txt', 'wb')
for key, value in scores_guessed.items():
writer.writerow([key,':',value])
很明显,它将打印出字典的以下结果:
Adam 150 140 130 50
Dave 120 110 80 60
Jack 100 90 70 40
但是,我希望文件按字母顺序向我读出最后四个结果:
Adam:150
Adam:140
Adam:130
Adam:50
Dave:120
Dave:110
Dave:80
Dave:60
Jack:100
Jack:90
Jack:70
Jack:40
我认为这段代码行得通:
for key, value in scores_guessed.items():
writer.writerow([key,':',value])
然而这个 returns 我的结果是:
AttributeError: '_io.BufferedWriter' object has no attribute 'writerow'
例如,如果亚当得了 200 分,我希望 scores_guessed 重写为:
Adam:200
Adam:150
Adam:140
Adam:130
出了什么问题?
UPDATE - 为了回应下面的第一个答案,我将最后一个代码块编辑为:
for key, value in scores_guessed.items():
writer.write("{}:{}\n".format(key,value))
然而它给了我这个信息: writer.write("{}:{}\n".format(key,value)) 类型错误:'str' 不支持缓冲区接口
发生了什么事?
你使用的writer.writerow
语法对应the csv module,用法如下:
import csv
with open('some_file.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['Spam'] * 5 + ['Baked Beans'])
writer.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
在我看来,您使用了一些错误的引用来执行此操作,因为您没有在上面的代码中使用 csv
模块。
因此,更改您的以下代码
for key, value in scores_guessed.items():
writer.writerow([key,':',value])
此代码:
for key, value in scores_guessed.items():
output = "%s:%s\n" % (key,value)
writer.write(output)
编辑
您正在以二进制模式打开文件,而不是使用
以文本模式打开文件writer = open('Guess Scores.txt', 'wt')
for key, value in scores_guessed.items():
output = "{}:{}\n".format(key,value)
writer.write(output)
writer.close()
编辑 2
既然你用的是双端队列,那就用:
writer = open('Guess Scores.txt', 'wt')
for key, value in scores_guessed.items():
output = "{}:{}\n".format(key,','.join(map(str, scores_guessed[key])))
writer.write(output)
writer.close()
或使用:
with open("output.txt", "wt") as f:
for key in scores_guessed:
f.write("{} {}".format(key, ','.join(map(str, scores_guessed[key]))))