How do I loop through all levels of a data structure to extract all data when I don't know how many levels there will be?
I need to extract data from a structure and put it into a list, but I don't know how many levels the structure has.
For each level, I can call level.children()
, if there are no levels below the current one, it returns []
, if there are, it returns [object, object, ...]
, on each of which I can call children()
on again.
I need to drill down through the structure until I've extracted all levels of data into a list.
When based off a structure like this:
<name>John Smith</name>
<team link="http://teamwebsite.com">
<name>Team Name</name>
</team>
<games>
<loca开发者_开发百科tion>
<venue>A stadium</venue>
</location>
</games>
The list should look something like this:
[
[
{'name': 'name', 'attrs': {}, 'text': 'John Smith', 'parent': None},
],
[
{'name': 'team', 'attrs': {'link': 'http://teamwebsite.com'}, 'text': '', 'parent': None},
{'name': 'name', 'attrs': {}, 'text': 'Team Name', 'parent': 1}, # the reference to its parent's position in the list
],
[
{'name': 'games', 'attrs': {}, 'text': '', 'parent': None},
{'name': 'location', 'attrs': {}, 'text': '', 'parent': 1},
{'name': 'venue', 'attrs': {}, 'text': 'A stadium', 'parent': 2},
],
]
I'm trying to figure out the Python I would use to get from the data structure to my list. I need a kind of self-perpetuating for loop, but I can't come up with a good solution.
Anything to point me in the right direction? I'm sure there is some good theory for this kind of thing that I completely don't know about but would be happy to read.
You're describing recursion, but I'm guessing there are better, ways, to, parse, XML.
The concept you're looking to use here is called "Recursion".
精彩评论