【发布时间】:2016-07-20 20:50:00
【问题描述】:
我上网查看并尝试解决此问题,并使用其他帖子中的解决方案来构建我的解决方案,但是从这里开始,我不知道下一步该做什么。
我基本上想获取 PastWinners 文本文件的最后 5 行,然后得到这些数字的平均值。我目前拥有的是整个文档的平均值,并打印出文本文件中的最后一行。
with open('PastWinners.txt') as f:
data = [float(line.rstrip()) for line in f]
first = f.readline() # Read the first line.
f.seek(-2, 2) # Jump to the second last byte.
while f.read(1) != b"\n": # Until EOL is found...
f.seek(-2, 1) # ...jump back the read byte plus one more.
last = f.readline() # Read last line.
biggest = min(data)
smallest = max(data)
print(sum(data)/len(data))
print(last)
感谢您的帮助。
【问题讨论】:
-
遍历 f 后,对
readline的调用将返回None。 -
数据是如何写入文件的?每行一个值?
标签: python file text numbers average