简体   繁体   中英

How to convert tree-like output of directory structure into a JSON

I have an output that's similar to a tree command, looks like this one here:

dir1 (820)
+-- dir2 (820)
    +-- dir3 (820)
        +-- dir4 (820)
            |-- file0 (0)
            |-- dir5 (795)
            |   |-- dir6 (464)
            |   |   |-- anotherDir1 (1)
            |   |   |   +-- anotherDir2 (1)
            |   |   |       +-- file1.cpp (1)
            |   |   |-- fir7 (40)
            |   |   |   |-- file2.c (2)
            |   |   |   |-- file3.c (2)
            |   |   |   |-- file4.c (20)
            |   |   |   |-- file5.c (1)
            |   |   |   |-- file6.c (1)
            |   |   |   |-- file7.c (4)
            |   |   |   |-- file8.c (4)
            |   |   |   +-- file9.c (6)
... * goes like that for a while *

I need to get that tree -like structure into a JSON. And I really don't know how to go about it. The numbers like 820 are values that have to be in the JSON. I have to do it in python

Assuming your output is in a string, you can use recursion:

import re, json
s = 'dir1 (820)\n+-- dir2 (820)\n    +-- dir3 (820)\n        +-- dir4 (820)\n            |-- file0 (0)\n            |-- dir5 (795)\n            |   |-- dir6 (464)\n            |   |   |-- anotherDir1 (1)\n            |   |   |   +-- anotherDir2 (1)\n            |   |   |       +-- file1.cpp (1)\n            |   |   |-- fir7 (40)\n            |   |   |   |-- file2.c (2)\n            |   |   |   |-- file3.c (2)\n            |   |   |   |-- file4.c (20)\n            |   |   |   |-- file5.c (1)\n            |   |   |   |-- file6.c (1)\n            |   |   |   |-- file7.c (4)\n            |   |   |   |-- file8.c (4)\n            |   |   |   +-- file9.c (6)'
data = [re.findall('^\W+|[\w\.]+|\(\d+\)', i) for i in filter(None, s.split('\n'))]
def to_tree(d):
   r, k = [], []
   for a, b, c in d:
      if not a:
         r.append({'name':b, 'num':c[1:-1], 'children':None if not k else to_tree(k)})
      else:
         k.append([a[4:], b, c])
   if k:
      r[-1]['children'] = to_tree(k)
   return r

result = to_tree([['', *i] if len(i) == 2 else i for i in data])
print(json.dumps(result, indent=4))

Output:

[
   {
      "name":"dir1",
      "num":"820",
      "children":[
         {
            "name":"dir2",
            "num":"820",
            "children":[
               {
                  "name":"dir3",
                  "num":"820",
                  "children":[
                     {
                        "name":"dir4",
                        "num":"820",
                        "children":[
                           {
                              "name":"file0",
                              "num":"0",
                              "children":"None"
                           },
                           {
                              "name":"dir5",
                              "num":"795",
                              "children":[
                                 {
                                    "name":"dir6",
                                    "num":"464",
                                    "children":[
                                       {
                                          "name":"anotherDir1",
                                          "num":"1",
                                          "children":"None"
                                       },
                                       {
                                          "name":"fir7",
                                          "num":"40",
                                          "children":[
                                             {
                                                "name":"anotherDir2",
                                                "num":"1",
                                                "children":"None"
                                             },
                                             {
                                                "name":"file2.c",
                                                "num":"2",
                                                "children":[
                                                   {
                                                      "name":"file1.cpp",
                                                      "num":"1",
                                                      "children":"None"
                                                   }
                                                ]
                                             },
                                             {
                                                "name":"file3.c",
                                                "num":"2",
                                                "children":[
                                                   {
                                                      "name":"file1.cpp",
                                                      "num":"1",
                                                      "children":"None"
                                                   }
                                                ]
                                             },
                                             {
                                                "name":"file4.c",
                                                "num":"20",
                                                "children":[
                                                   {
                                                      "name":"file1.cpp",
                                                      "num":"1",
                                                      "children":"None"
                                                   }
                                                ]
                                             },
                                             {
                                                "name":"file5.c",
                                                "num":"1",
                                                "children":[
                                                   {
                                                      "name":"file1.cpp",
                                                      "num":"1",
                                                      "children":"None"
                                                   }
                                                ]
                                             },
                                             {
                                                "name":"file6.c",
                                                "num":"1",
                                                "children":[
                                                   {
                                                      "name":"file1.cpp",
                                                      "num":"1",
                                                      "children":"None"
                                                   }
                                                ]
                                             },
                                             {
                                                "name":"file7.c",
                                                "num":"4",
                                                "children":[
                                                   {
                                                      "name":"file1.cpp",
                                                      "num":"1",
                                                      "children":"None"
                                                   }
                                                ]
                                             },
                                             {
                                                "name":"file8.c",
                                                "num":"4",
                                                "children":[
                                                   {
                                                      "name":"file1.cpp",
                                                      "num":"1",
                                                      "children":"None"
                                                   }
                                                ]
                                             },
                                             {
                                                "name":"file9.c",
                                                "num":"6",
                                                "children":[
                                                   {
                                                      "name":"file1.cpp",
                                                      "num":"1",
                                                      "children":"None"
                                                   }
                                                ]
                                             }
                                          ]
                                       }
                                    ]
                                 }
                              ]
                           }
                        ]
                     }
                  ]
               }
            ]
         }
      ]
   }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM