开发者

How can I use XPATH to add data of subtree to main tree in Python/Django

I'm using etree to parse an external xml file and trying to get the listing data from a tree from the below external xml file and add the subtree agancy data to it. I am able to pull the data for isting and agancy just fine seperately, but don't know how to merge them so that the listing gets the correct agency info.

xml:

<response>
    <listing>
        <bathrooms>2.1</bathrooms>
        <bedrooms>3</bedrooms>
        <agency>
            <name>Bob's Realty</name>
            <phone>555-693-4356</phone>
        </agency>
    </listing>
    <listing>
        <bathrooms>3.1</bathrooms>
        <bedrooms>5</bedrooms>
        <agency>
            <name>Larry's Homes</name>
            <phone>555-324-6532</phone>
        </agency>
    </listing>
</response>

python:

tree = lxml.etree.parse("http://www.someurl.com?random=blahblahblah")
listings = tree.xpath("/response/listing")
agencies = tree.xpath("/response/listing/agency")

listings_info = []

for listing in listings:
    this_value = {
        "bedrooms":listing.findtext("bedrooms"),
        "bathrooms":listing.findtext("bathrooms"),
        }

        for agency in agencies:
            this_value['agency']= agency.findtext("name")


    listings_info.append(this_value)

I tried adding this at one point just above where the listing_info.append(this_value) occurs, however this is not correct and just appends the last agency value to every listing.

I'm outputting the data into json and here's what it looks like (You can see how one agency's info is being put into both results:

    {"listing开发者_如何学JAVAs":[{"agency": "Bob's Realty", "phone":"555-693-4356" "bathrooms": "2.1", "bedrooms": "3"},{"agency": "Bob's Realty", "phone":"555-693-4356" "bathrooms": "3.1", "bedrooms": "5"} ]}

How can I merge the data from response/listing/agency with response/listing in my original for statement?


You can use listing.xpath('agency/name/text()')[0] as you iterate through your list to get the agency's name for just that listing.

for listing in listings:
    this_value = {
        'bedrooms': listing.findtext('bedrooms'),
        'bathrooms': listing.findtext('bathrooms'),
        'agency': listing.xpath('agency/name/text()')[0]
    }
    listings_info.append(this_value)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜