tree2json
将树形目录结构转换为结构化的 JSON 格式。
tree2json 是一个轻量级 Python 工具,用于解析目录树格式的文本(例如 tree 命令的输出或自定义目录结构字符串),并将其转换为层次化的 JSON 结构,适用于可视化、自动化或结构分析任务。
📦 安装
从 PyPI 安装:
pip install tree2json
或克隆本地进行开发使用:
git clone https://github.com/Knighthood2001/tree2json.git
cd tree2json
pip install -e .
🧩 功能特色
- 支持解析纯文本目录树结构
- 支持多级嵌套的文件和文件夹
- 输出结构化 JSON,包含完整目录层级信息
🚀 快速使用
Python 示例
(PS:也可以查看tests/test.py)
from tree2json import Tree2Json
tree_str = """
.
├── data
│ ├── data.zip
│ └── results
├── README.md
└── utils
├── denoise.py
└── transforms.py
"""
converter = Tree2Json()
converter.from_string(tree_str)
json_str = converter.to_json_file("output.json") # 保存文件并返回字符串
输出示例
{
"level": 0,
"type": "dir",
"name": ".",
"description": "",
"child": [
{
"level": 1,
"type": "dir",
"name": "data",
"description": "",
"child": [
{
"level": 2,
"type": "file",
"name": "data.zip",
"description": "",
"child": []
},
{
"level": 2,
"type": "dir",
"name": "results",
"description": "",
"child": []
}
]
},
{
"level": 1,
"type": "file",
"name": "README.md",
"description": "",
"child": []
},
{
"level": 1,
"type": "dir",
"name": "utils",
"description": "",
"child": [
{
"level": 2,
"type": "file",
"name": "denoise.py",
"description": "",
"child": []
},
{
"level": 2,
"type": "file",
"name": "transforms.py",
"description": "",
"child": []
}
]
}
]
}
🔧 API 接口说明
Tree2Json(mode="auto")
mode:缩进识别模式(auto|step3|step4)
.from_string(tree_str)
从树状字符串中解析目录结构。
.to_dict()
以 Python 字典形式返回目录结构。
.to_json_file(path=None)
将结构保存为 JSON 文件(可选)。始终返回 JSON 字符串。
📄 许可证
本项目使用 MIT 开源许可证。
💬 问题反馈 & 贡献
欢迎在 issue 页面 提出问题或提交 PR!
TODO
- [√] 项目根目录的完善
- 适配更多类型的目录树
- [√] 将目录树注释提取出来,支持 ← # // -- 作为注释分隔符
版本
v0.1.0
- 初始版本
v0.1.1
- 调整了README.md
v0.1.2
- [√] 项目根目录的完善
v0.1.3
- 支持 ← # // -- 作为注释分隔符
v0.1.4
- 解决根目录结尾为/或者\的问题
Metadata
Release files for tree2json 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tree2json-0.1.4.tar.gz | 5.3 kB | Without provenance | Details for tree2json-0.1.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tree2json-0.1.4-py3-none-any.whl | Python 3 | none | any | Details for tree2json-0.1.4-py3-none-any.whl |
Total release size: 9.5 kB
Release files / tree2json-0.1.4.tar.gz
| Download URL | tree2json-0.1.4.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c98e141994c63f0e5deaa4c5ba8ec8369e9f9063e00238b7c226d83a9b154963
|
|
BLAKE2b-256 checksum How to use checksums |
17f979e2e97dc11c91412c1749b9fafa708556850381b3d560ee85ab958d6534
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|
Release files / tree2json-0.1.4-py3-none-any.whl
| Download URL | tree2json-0.1.4-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
72e3c7f633f293ec75d5527d1cb645bf697598390c7fb34da07384fbab2b1773
|
|
BLAKE2b-256 checksum How to use checksums |
33c263597ebbbcd7be3685dc5eeea5887021eebf053bce09c43f1e642a58e438
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|
Security for tree2json 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.