简体   繁体   English

使用Python查询XML子代

[英]Query XML subchild with Python

I'm using Python to query my Tableau server using the API. 我正在使用Python使用API​​查询我的Tableau Server。 I'm pulling the revision history of each workbook, and trying to see which user made each edit. 我正在提取每个工作簿的修订历史记录,并尝试查看哪个用户进行了每个编辑。 Within the revision element, there is a sub-element for the user. 在修订元素内,有一个用户子元素。 I'm trying to understand how to pull the username from this sub-element and associate it with the correct revision data. 我试图了解如何从该子元素中提取用户名,并将其与正确的修订数据相关联。

Here is an example line of the xml data returned: 这是返回的xml数据的示例行:

<tsResponse>
  <revisions>
    <revision revisionNumber="1" publishedAt="2017-01-17 T20:43:40Z" 
     deleted="false" current="true" sizeInBytes="1939262">
    <publisher id="53f212f3" name="1563524" />
    </revision>
  </revisions>
</tsresponse>

The element containing id and name is where I'm trying to get the data. 包含id和name的元素是我要获取数据的位置。 I'm able to pull it into a second dictionary list, but I need all the data to be in a single dictionary so I can parse the headers and output to a csv. 我可以将其拉入第二个词典列表,但是我需要所有数据都放在一个词典中,这样我才能解析标头并将其输出到csv。 Here's the relevant bit of code I've written: 这是我编写的相关代码:

Pub = []
Rev = []
RevDet = []
revisions = server_response_WB2.findall('.//t:revision', namespaces=xmlns)
publishers = server_response_WB2.findall('.//t:publisher', namespaces=xmlns)
for revision in revisions:
    d = {}
    d['workbook id'] = workbookid[i]
    d['workbook name'] = workbooknames[i]
    d['revision number'] = revision.get('revisionNumber')
    d['revision created at'] = revision.get('publishedAt')
    d['is deleted'] = revision.get('deleted')
    d['is current'] = revision.get('current')
    Rev.append(d)
for publisher in publishers:
    d = {}
    d['user name'] = publisher.get('name')
    Pub.append(d)
RevDet.append(Rev[i])
RevDet.append(Pub[i])

As you can see, at the end I'm appending both dictionary lists to RevDet, so the data is closely associated. 如您所见,最后,我将两个字典列表都附加到RevDet,因此数据紧密相关。 For example, element 0 and element 1 of RevDet will contain the associated revision and publisher data respectively. 例如,RevDet的元素0和元素1将分别包含关联的修订和发布者数据。 Any ideas on how to handle this better? 关于如何更好地处理此问题的任何想法? What should my next step be? 我下一步应该做什么?

revision.find('t:publisher', namespaces=xmlns)为您提供了包含的元素,因此只需在第一个for语句中使用revision.find('t:publisher', namespaces=xmlns).get('name')提取值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM