I'm attempting to read values from a file and then sort them, and then rewriting the values back to the file. The file contains the following syntax for the value: NAME, SCORE/10
. Each new value begins on a new line e.g:
TEST, 5/10
TEST2, 2/10
Currently, I have the sorting of A - Z working. However, I am having difficulty sorting via the values. Could anybody help me with sorting from Highest Num - Lowest Num? (10 - 0)
The code I have currently to read the Values is this:
def splitList(alist):
a = []
i = 0
for i in range(alist.count("\n")):
a = a.append(alist[:])
i =+ 1
return a
And this is what I have to sort from Highest to Lowest:
def sortFile(filePath, mode = "ABC"):
if mode == "321":
try:
f = open(filePath, "r")
try:
lines = f.readlines()
print(lines)
lines = lines.sort(key=lambda x: x[2], reverse=True)
print(lines)
f.close()
f = open(filePath, "w")
f.writelines(lines)
finally:
f.close()
except IOError:
pass
return True