Nov-03-2019, 02:45 PM
(This post was last modified: Nov-03-2019, 02:55 PM by AlekseyPython.)
If your txt- file look like this (you made a mistake in the last line, forgetting the number):
then you can do this:
Quote:13,pops
15,bubble
150,nano
33,party
11,pokey
then you can do this:
def getIt(): with open("Text File.txt", "r")as file: data = file.read().splitlines() data.sort(key=lambda x: int(x.split(",")[0]), reverse = True) with open("Text File.txt", "w")as file: for element in data: file.write(element + '\n') getIt()
Output:150,nano
33,party
15,bubble
13,pops
11,pokey