使用etree解析xml时无法返回子值

时间:2020-03-20 00:15:45

标签: python xml elementtree

我有一个看起来像的xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<FullReport
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <firm>1426</firm>
    <reportDate>07FEB2020_18:00:00.000000</reportDate>
    <rooms>
        <room>
            <roomID>PCHAT-0x0000000000000637</roomID>
            <roomTitle>FX - WBB - CTON</roomTitle>
            <description>global chat</description>
            <creationDate></creationDate>
            <removalDate></removalDate>
            <lastActivityDate>02/07/2020 12:26:24</lastActivityDate>
            <status>Active</status>
            <membership>Bilateral</membership>
            <isAnonymous>false</isAnonymous>
            <hasActiveAdmins>true</hasActiveAdmins>
            <activeUserCount>17</activeUserCount>
            <distinctFirmsInRoom>2</distinctFirmsInRoom>
            <isInternalOnly>false</isInternalOnly>
            <isIncognitoForum>false</isIncognitoForum>
        </room>
        <users>
            <uuid>6820</uuid>
            <bbgEmail>SJONES@Bloomberg.net</bbgEmail>
            <fullName>SEAN JONES</fullName>
            <firmName>BANK OF TEST</firmName>
            <firmNumber>1400</firmNumber>
            <accountNumber>51067</accountNumber>
            <accountName>BANK OF TEST</accountName>
            <inviteDate>01/07/2013 22:00:39</inviteDate>
            <isDeleted>false</isDeleted>
            <isAdmin>false</isAdmin>
            <isCreator>false</isCreator>
            <roomAlias>CTON</roomAlias>
            <corpEmail>sean.jones@botest.com</corpEmail>
            <city>LONDON</city>
        </users>
        <users>
            <uuid>6820</uuid>
            <bbgEmail>SSMITH@Bloomberg.net</bbgEmail>
            <fullName>SEAN SMITH</fullName>
            <firmName>BANK OF TEST</firmName>
            <firmNumber>1400</firmNumber>
            <accountNumber>51067</accountNumber>
            <accountName>BANK OF TEST</accountName>
            <inviteDate>01/07/2013 22:00:39</inviteDate>
            <isDeleted>false</isDeleted>
            <isAdmin>false</isAdmin>
            <isCreator>false</isCreator>
            <roomAlias>CTON</roomAlias>
            <corpEmail>sean.smith@botest.com</corpEmail>
            <city>LONDON</city>
        </users>
</FullReport>
</FullReport>

我正在使用以下代码解析数据:

import xml.etree.cElementTree as et

tree = et.parse('test.xml')
print (tree.getroot())
root = tree.getroot()
print ("tag=%s, attrib=%s" % (root.tag, root.attrib))

for child in root:
        print (child.tag, child.attrib)
        if child.tag == "room":
            for step_child in child:
                print (step_child.tag)

# get the information via the children!
print ("-" * 40)
print ("Iterating using a getchildren()")
print ("-" * 40)
rooms = root.getchildren()
for room in rooms:
    room_children = room.getchildren()
    for room_child in room_children:
        print ("%s=%s" % (room_child.tag, room_child.text))

当我打印room_child.tag和room_child.text时,我看不到值吗?对此很陌生,所以我不确定我缺少什么吗?

返回的结果是:

enter image description here

我最终的目标是遍历每个值并转换为CSV,但是我一直无法访问这些值,并且不确定为什么没有数据返回。我希望最终的csv看起来类似于csv中每个用户的一行

enter image description here

1 个答案:

答案 0 :(得分:1)

运行for child in root:时,此循环仅迭代 FullReport 的直接后代中,在您的情况下: firm reportDate rooms ,因此没有机会进入 room ,即 位于下一层。

从您的代码(第一个循环)中,我看到您实际上有兴趣 在 FullReport / rooms / room 的直接后代中。

要打印其标签名称和文本内容,可以运行例如:

for child in root.iter('room'):
    for step_child in child:
        print(f'{step_child.tag:20} {step_child.text}')

对于您的示例输入,结果为:

roomID               PCHAT-0x0000000000000637
roomTitle            FX - WBB - CTON
description          global chat
creationDate         None
removalDate          None
lastActivityDate     02/07/2020 12:26:24
status               Active
membership           Bilateral
isAnonymous          false
hasActiveAdmins      true
activeUserCount      17
distinctFirmsInRoom  2
isInternalOnly       false
isIncognitoForum     false

就您的最终任务而言,您可以运行:

rows = []
for child in root.iter('rooms'):
    roomId, roomTitle = 'id', 'ttl'
    for it in child:
        if it.tag == 'room':
            roomId = it.findtext('roomID')
            roomTitle = it.findtext('roomTitle')
        elif it.tag == 'users':
            rows.append([roomId, roomTitle, it.findtext('uuid'), it.findtext('bbgEmail'),
                it.findtext('fullName'), it.findtext('firmName')])
df = pd.DataFrame(rows, columns=['roomId', 'roomTitle', 'uuid', 'bbgEmail',
    'fullName', 'firmName'])

上面的代码基于 room 元素首先出现的假设 在它们之后有 users 个元素。

并添加与您的帖子中未显示的其他任何列有关的代码。