I have a flat text file that represents a hierarchy. It looks similar to this:
0 tom (1)
1 janet (8)
2 harry (1)
3 jules (1)
3 jacob (1)
1 mary (13)
2 jeff (1)
3 sam (2)
1 bob (28)
2 dick (1)
I want to read this in and build a nested dictionary (or some kind of data structure) to represent the hierarchy so it is easier to manage but I can't wrap my head around how to iterate and create a data structure. Maybe recursion?
The first number is the level of the hierarchy, the word is the name I want to store and the value in the parenthesis is the quantity that I also want to store.
I'd like to end up with something similar to this:
{
"tom": {
"quantity": 1,
"names": {
"janet": {
"quantity": 8,
"names": {
"harry": {
"quantity": 1,
"names": {
"jules": {
"quantity": 1
},
"jacob": {
"quantity": 1
}
}
}
}
},
"mary": {
"quantity": 13,
"names": {
"jeff": {
"quantity": 1,
"names": {
"sam": {
"quantity": 2
}
}
}
}
},
"bob": {
"quantity": 28,
"names": {
"dick": {
"quantity": 1
}
}
}
}
}
}