Python-如何按第一和第二键值对字典排序?

我正在处理文本文件“ creatures.txt”。其内容的示例可以在下面看到:

Special Type A Sunflower
2016-10-12 18:10:40
Asteraceae
Ingredient in Sunflower Oil
Brought to North America by Europeans
Requires fertile and moist soil
Full sun

Pine Tree
2018-12-15 13:30:45
Pinaceae
Evergreen
Tall and long-lived
Temperate climate

Tropical Sealion
2019-01-20 12:10:05
Otariidae
Found in zoos
Likes fish
Likes balls
Likes zookeepers

Big Honey Badger
2020-06-06 10:10:25
Mustelidae
Eats anything
King of the desert

将其内容转换为字典输入的值时,它运行良好。
输入

def TextFileToDictionary():
    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:]          
            })
        return dataset                          
TextFileToDictionary()


输出

[{'Name': 'Special Type A Sunflower','Date': '2016-10-12 18:10:40','Information': ['Asteraceae','Ingredient in Sunflower Oil','Brought to North America by Europeans','Requires fertile and moist soil','Full sun']},{'Name': 'Pine Tree','Date': '2018-12-15 13:30:45','Information': ['Pinaceae','Evergreen','Tall and long-lived','Temperate climate']},{'Name': 'Tropical Sealion','Date': '2019-01-20 12:10:05','Information': ['Otariidae','Found in zoos','Likes fish','Likes balls','Likes zookeepers']},{'Name': 'Big Honey Badger','Date': '2020-06-06 10:10:25','Information': ['Mustelidae','Eats anything','King of the desert']}]

观察到,输出包括多个字典,没有名称。

目前,我正在尝试创建将字典按
1)第一键值按字母顺序排序和
2)第二键值按最新日期排序的函数。

我的进度是:

import itertools
import os

MyFilePath = os.getcwd() 
ActualFile = "creatures.txt"
FinalFilePath = os.path.join(MyFilePath,ActualFile) 

def TextFileToDictionaryName():
    dataset = [] 
    with open(FinalFilePath,"Information": lines[2:]          
            })
            dataset.sort(key=lambda x: x[0]['Name'],reverse=False)
        return dataset                          
TextFileToDictionaryName()

def TextFileToDictionaryDate():
    dataset = [] 
    with open(FinalFilePath,"Information": lines[2:]          
            })
            dataset.sort(key=lambda x: x[1]['Date'],reverse=True)
        return dataset                          
TextFileToDictionaryDate()

但是,我遇到了错误“ KeyError:0”。我不确定如何解决。
我也不确定要将字典输出转换回字符串格式,就像前面“ creatures.txt”文件中的内容一样。

有人知道如何修复代码吗?

非常感谢!

相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...