A lightweight tool to visualize the structure of nested Python dictionaries and lists as a tree.
Project description
Dictree
dictree 是一个轻量级的 Python 工具,用于以树状结构可视化复杂的嵌套字典(Dict)、列表(List)、元组(Tuple)和集合(Set)。它旨在帮助开发者快速了解复杂数据结构的 Schema (架构),而不会被具体的数值所淹没。
🚀 特性
- 结构化展示:模仿 Unix
tree命令的样式展示嵌套结构。 - 智能代表提取:对于列表或集合,自动识别其中的元素类型。如果列表中包含字典或嵌套容器,会提取其结构作为代表。
- 深度控制:通过
max_depth参数防止无限展开或处理超大型对象。 - 自定义模板:可以自定义列表/容器的统计信息展示格式。
- 输出灵活:支持直接打印到控制台或返回字符串。
📦 安装
目前可以通过源码安装:
pip install .
🛠️ 快速上手
1. 基本用法
直接打印一个复杂的嵌套字典:
from dictree import sprint
data = {
"users": [
{
"id": 1,
"profile": {
"name": "Alice",
"hobbies": ["coding", "reading"]
}
},
{
"id": 2,
"profile": {
"name": "Bob",
"hobbies": ["gaming"]
}
}
],
"status": "success",
"meta": {"page": 1, "total": 100}
}
sprint(data)
输出:
├── users
│ └── [2]: dict
│ ├── id
│ └── profile
│ ├── name
│ └── hobbies
│ └── [2]: str
├── status
└── meta
├── page
└── total
2. 限制展开深度
当对象非常庞大时,可以使用 max_depth 参数:
sprint(data, max_depth=1)
输出:
├── users
│ └── … (depth limit)
├── status
└── meta
└── … (depth limit)
3. 获取字符串而不是打印
如果你需要将结果保存到日志或文件中:
tree_str = sprint(data, return_string=True)
print(f"Captured Tree:\n{tree_str}")
4. 自定义列表格式
你可以利用 list_repr_template 改变列表项的显示方式。
默认模板为 "{count}: {repr}"。
sprint(data, list_repr_template="[{count} elements of {repr}]")
输出示例:
├── users
│ └── [2 elements of dict]
...
💡 为什么使用 dictree?
在处理爬虫返回的 JSON、大型 API 响应或复杂的配置文件时,print() 或 pprint 往往会输出成千上万行数据。dictree 通过隐藏具体的“值”,只提取“键”和“容器类型”,让你在一屏之内看清数据的所有层级和结构。
📄 开源协议
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dictree_sprint-0.1.0.tar.gz.
File metadata
- Download URL: dictree_sprint-0.1.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ef4bbb339a4751ab2b5a03c7848276d250b6d8580740190643b52cd33bd84ed
|
|
| MD5 |
e59d545c123e656d183960637f6f5faf
|
|
| BLAKE2b-256 |
2cf40ca2c706a3e592dab0fcaa0bc843ae02128556fd9a4b456880cf456b239a
|
File details
Details for the file dictree_sprint-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dictree_sprint-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89c24c73ce2816c85673334184139ae135ca739e1083e2237f877f675b5c6412
|
|
| MD5 |
cc3e17908830c56ce08892075eafd41a
|
|
| BLAKE2b-256 |
44e20d941a903b5270bf78b2581beb7af0888b83029f7b47d68df6fe41553c5e
|