如何使用 XPATH 将子树的数据添加到 Python/Django 中的主树

发布于 2024-12-05 23:24:23 字数 1871 浏览 1 评论 0原文

我正在使用 etree 解析外部 xml 文件,并尝试从下面的外部 xml 文件中的树中获取列表数据,并向其中添加子树agancy 数据。我能够分别提取 istingagancy 的数据,但不知道如何合并它们,以便 listing 得到正确的机构信息。

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)

我尝试在 listing_info.append(this_value) 出现的位置上方添加此内容,但是这是不正确的,只是将最后一个机构值附加到每个列表中。

我将数据输出到 json 中,如下所示(您可以看到一个机构的信息如何放入两个结果中:

    {"listings":[{"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 <我原来的 for 语句中的 code>response/listing ?

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:

    {"listings":[{"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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丿*梦醉红颜 2024-12-12 23:24:23

您可以在迭代列表时使用 listing.xpath('agency/name/text()')[0] 来获取该列表的代理机构名称。

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文