Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Phasing a tabbed file?
#1
Hi there,

I am having trouble with what I believe is a very simple problem. I have a text file which looks like this:
Cat 1
    Sub Cat 1
    Sub Cat 2
    Sub Cat 3
        Testing
Cat 2
    Sub Cat 1
        Nested item
        Another nested item
            Nested-nested item
            Nested-nested item 2
        Yet another nested item
    Sub Cat 2
    Sub Cat 3

The lines end in \n and four spaces represent a tab (\t - I couldn't add tabs here). I am trying to convert this list of items into a nested array with a similar structure to that of a file system.

Ideally, the result should look something like this:
{"Cat 1": {
    "Sub Cat 1",
    "Sub Cat 2",
    {"Sub Cat 3": {"Testing"}}
},
"Cat 2": {
    {"Sub Cat 1": {"Nested item",
                        {"Another nested item": {"Nested nested item", "Nested nested item 2"}},
                        "Yet another nested item"},
    "Sub Cat 2",
    "Sub Cat 3"}
}

Afterwards, I would like to convert the nested array into both JSON and CSV format to be written to the file system. Please feel free to change the format of the result and nested results, so long as they can be outputted correctly.
Reply


Messages In This Thread
Phasing a tabbed file? - by Joseph_f2 - Dec-13-2017, 09:35 PM
RE: Phasing a tabbed file? - by j.crater - Dec-13-2017, 09:51 PM
RE: Phasing a tabbed file? - by Joseph_f2 - Dec-14-2017, 01:43 PM
RE: Phasing a tabbed file? - by ODIS - Dec-14-2017, 12:44 AM
RE: Phasing a tabbed file? - by Joseph_f2 - Dec-14-2017, 11:44 PM
RE: Phasing a tabbed file? - by ODIS - Dec-15-2017, 03:53 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020