with open('winnernum.txt', 'r') as b:
data = b.readlines()
gone=(max(data))
print(gone)
with open("winnernum.txt","r") as h:
del gone
I've tried this and other variations of this code in python but it still won't delete. I need to print the top 5 largest numbers from a text file.
I've attempted using this before:
with open('winners.txt', 'r') as b:
data = b.readlines()
gone=(max(data))
print(gone)
import heapq
print(heapq.nlargest(5, winner))
but that doesn't always pick the top 5 numbers and tends to select them at random. Please help!
Here is a simple solution:
from heapq import nlargest
with open("winnernum.txt", "r") as f:
numbers = [float(line.rstrip()) for line in f.readlines()]
largest = nlargest(5, numbers)
print(largest)
Try this:
from contextlib import closing
with closing(open('winners.txt', 'r')) as file:
gone = max(map(lambda x: x.rstrip('\n'), file.readlines()))
print(gone)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.