将数据字典输出树形结构

时间: 2023-10-28 08:06:14 浏览: 45
可以使用递归函数实现将数据字典输出为树形结构。以下是一个简单的示例代码: ```python def print_tree(data, level=0): for key, value in data.items(): print(" " * level + str(key)) if isinstance(value, dict): print_tree(value, level+1) else: print(" " * (level+1) + str(value)) ``` 这个函数接受一个数据字典和一个可选的级别参数。它首先输出当前级别的键,然后检查该键对应的值是否也是一个字典。如果是,它递归地调用自身,将该值作为新的数据字典和级别+1作为新的级别传递。如果不是,它只是输出值。 你可以将你的数据字典传递给这个函数,例如: ```python data = { "animals": { "mammals": { "dogs": ["Beagle", "Boxer", "Dalmatian"], "cats": ["Siamese", "Persian"] }, "birds": { "parrots": ["African Grey", "Amazon", "Cockatoo"], "canaries": ["Red Factor", "Spanish Timbrado"] } }, "plants": { "flowers": { "roses": ["Climbing", "Floribunda", "Hybrid Tea"], "tulips": ["French", "Parrot", "Rembrandt"] }, "trees": { "deciduous": ["Maple", "Oak", "Birch"], "evergreen": ["Pine", "Spruce", "Fir"] } } } print_tree(data) ``` 输出结果将是: ``` animals mammals dogs Beagle Boxer Dalmatian cats Siamese Persian birds parrots African Grey Amazon Cockatoo canaries Red Factor Spanish Timbrado plants flowers roses Climbing Floribunda Hybrid Tea tulips French Parrot Rembrandt trees deciduous Maple Oak Birch evergreen Pine Spruce Fir ```

相关推荐

import csv import requests from lxml import etree #爬取的页面 url = "https://movie.douban.com/explore" #定义请求头 headers = { "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" } #获取html字符串 response = requests.get(url,headers=headers) content = response.content.decode('utf8') #将html字符串解析 html = etree.HTML(content) #Xpath获取总框架 trs = html.xpath('//div[@class="drc-subject-info"]') #定义一个列表来存储数据 mv_data = [] #for循环遍历总框架,for循环内部的.指的是当前框架下的内容匹配 for tr in trs: #定义一个集合存储 eg = {} #在上面的框架下爬取电影的标题 title = tr.xpath('./div[@class="drc-subject-info-title"]/span/text()')[0].strip() #在上面的框架下爬取电影的年份、类型、主演 information = tr.xpath('./div[@class="drc-subject-info-title"]/div[@class="drc-subject-info-subtitle"]/text()')[0].strip() #在上面的框架下爬取电影的评分 score = tr.xpath('./div[@class="drc-rating drc-subject-info-rating m"]/span[@class="drc-rating-num"]/text()')[0].strip() #定义一个字段来存储key,value的结构 eg = { "title":title, "information":information, "score":score } #在hots列表中添加eg字典的内容 mv_data.append(eg) with open("mv_data.csv","a",encoding="utf8",newline="") as f: #设置字段名(列表名) filenames = ['title','information','score'] #定义开头 writer = csv.DictWriter(f,fieldnames=filenames) #写入开头 writer.writeheader() # 打印 print(mv_data) #内容 writer.writerows(mv_data) #打印写入完成 print("数据已写入")

最新推荐

recommend-type

智能停车收费系统数据流程图和数据字典

智能停车场收费系统数据流程图 智能停车场收费系统数据字典 1数据流词条 2数据元素词条 3数据文件词条 4加工词条 5外部实体
recommend-type

用友政务数据字典——数据基础结构

是一个很有用的数据结构表,对了解用友政务软件数据库表间结构有很大的帮助。
recommend-type

国家医院数据上报管理方案-医疗数据字典.docx

整理的卫健委的医疗行业的标准字典数据,例如医院的区域数据中心、集成平台之类的数据标准化字典数据!标准的医疗行业标准数据字典,特别是要过互联互通的等级非常实用!
recommend-type

OA系统-A8-V7.1SP1数据字典.pdf

企业OA软件,致远OA办公系统的,A8-V7版本的数据字典,PDF版本,可供DBA,OA管理员等人士使用
recommend-type

Python实现简单字典树的方法

主要介绍了Python实现简单字典树的方法,实例分析了Python字典树的定义、实现与使用技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。