-1

I have a string of tab-indented lines that looks like this:

Node A
    Node B
    Node C
        Node D
    Node E
Node F

And I hope to get a data structure where I can traverse the tree by the following:

print data[Node A][Node B][Node C] => Gives me all children under Node C

How can I do this with Python? I don't know what to search for this! Any help would be really appreciated.

4
  • 1
    rosettacode.org/wiki/Tree_traversal Commented Jul 27, 2014 at 21:01
  • 3
    Have you tried anything? It's a good idea to at least post an attempt. Commented Jul 27, 2014 at 21:30
  • possible duplicate of Python file parsing: Build tree from text file Commented Jul 27, 2014 at 22:02
  • 1
    Yes I have been looking/trying at things online for sometime.. It is a bit discouraging to not get any help or a push in the right direction, Commented Jul 27, 2014 at 23:42

2 Answers 2

3

You should look at networkx. It is a great library for graph / tree Data Structures.

And for your question on how to parse the tab separated data into a tree structure check this SO Question to help you get started.

Sign up to request clarification or add additional context in comments.

Comments

0

As a start, nested dictionaries could do the job:

tree = {
    "a": 5,
    "b": {
        "c": 6
    }
}

node = tree["b"]["c"]

2 Comments

I think he actually wants to know how to convert his tab-indented plain text file to a python dictionary.
Ah, sorry, I thought it was just an example of the data in the tree.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.