Python - How to sort dictionaries by first and second key values?
ef TextFileToDictionaryName():
dataset = []
with open(FinalFilePath, "r") as textfile:
sections = textfile.read().split("\n\n")
for section in sections:
lines = section.split("\n")
dataset.append({
"Name": lines[0],
"Date": lines[1],
"Information": lines[2:]
})
dataset.sort(key=lambda x: x['Name'], reverse=False)
return dataset
TextFileToDictionaryName()
def TextFileToDictionaryDate():
dataset = []
with open(FinalFilePath, "r") as textfile:
sections = textfile.read().split("\n\n")
for section in sections:
lines = section.split("\n")
dataset.append({
"Name": lines[0],
"Date": lines[1],
"Information": lines[2:]
})
dataset.sort(key=lambda x: x['Date'], reverse=True)
return dataset
TextFileToDictionaryDate()
No comments
Post a Comment