开发者

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?

开发者 https://www.devze.com 2022-12-09 21:29 出处:网络
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.

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".

0

精彩评论

暂无评论...
验证码 换一张
取 消