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],"@R_537_4045@ion": lines[2:]          
            })
        return dataset                          
TextFiletoDictionary()


输出

[{'Name': 'Special Type A Sunflower','Date': '2016-10-12 18:10:40','@R_537_4045@ion': ['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','@R_537_4045@ion': ['Pinaceae','Evergreen','Tall and long-lived','Temperate climate']},{'Name': 'Tropical Sealion','Date': '2019-01-20 12:10:05','@R_537_4045@ion': ['Otariidae','Found in zoos','Likes fish','Likes balls','Likes zookeepers']},{'Name': 'Big Honey Badger','Date': '2020-06-06 10:10:25','@R_537_4045@ion': ['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,"@R_537_4045@ion": lines[2:]          
            })
            dataset.sort(key=lambda x: x[0]['Name'],reverse=False)
        return dataset                          
TextFiletoDictionaryName()

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

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

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

非常感谢!

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...