Skip to main content

Convert tree-style text structure into JSON format

Project description

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!

版本

v0.1.0

  • 初始版本

v0.1.1

  • 调整了README.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tree2json-0.1.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tree2json-0.1.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file tree2json-0.1.1.tar.gz.

File metadata

  • Download URL: tree2json-0.1.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.2

File hashes

Hashes for tree2json-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6f841b0a6897852293203f8433a0601bac119742294952d99b9d22ab916d9d9c
MD5 5e7133fbb4e0402d6278edb1047c812b
BLAKE2b-256 ee7b6ba0db3926dfb21c049434dc71b3c3964263c1a211883ab2b817e5e55069

See more details on using hashes here.

File details

Details for the file tree2json-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tree2json-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.2

File hashes

Hashes for tree2json-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 22bce71dbe2f924c6737a48a83bcbcc0c48b50ba801c99fe621fbd02eca159e5
MD5 7ca374d34359cf59e8653b3a5520e418
BLAKE2b-256 c63b038a052a6ecf477b44976865a246ec7331dbcd6b4b8896f03e71d0258333

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page