如何使用 XPATH 将子树的数据添加到 Python/Django 中的主树
我正在使用 etree 解析外部 xml 文件,并尝试从下面的外部 xml 文件中的树中获取列表数据
,并向其中添加子树agancy
数据。我能够分别提取 isting
和 agancy
的数据,但不知道如何合并它们,以便 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在迭代列表时使用
listing.xpath('agency/name/text()')[0]
来获取该列表的代理机构名称。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.