gpt4 book ai didi

python - 在python中解析一个特殊的xml

转载 作者:数据小太阳 更新时间:2023-10-29 02:50:06 28 4
gpt4 key购买 nike

我有一个特殊的 xml 文件,如下所示:

<alarm-dictionary source="DDD" type="ProxyComponent">

<alarm code="402" severity="Alarm" name="DDM_Alarm_402">
<message>Database memory usage low threshold crossed</message>
<description>dnKinds = database
type = quality_of_service
perceived_severity = minor
probable_cause = thresholdCrossed
additional_text = Database memory usage low threshold crossed
</description>
</alarm>

...
</alarm-dictionary>

我知道在 python 中,我可以通过以下方式获取标签 alarm 中的“警报代码”、“严重性”:

for alarm_tag in dom.getElementsByTagName('alarm'):
if alarm_tag.hasAttribute('code'):
alarmcode = str(alarm_tag.getAttribute('code'))

而且我可以在标签 message 中获取文本,如下所示:

for messages_tag in dom.getElementsByTagName('message'):
messages = ""
for message_tag in messages_tag.childNodes:
if message_tag.nodeType in (message_tag.TEXT_NODE, message_tag.CDATA_SECTION_NODE):
messages += message_tag.data

但我也想获得,如dnkind(数据库)、type(quality_of_service)、perceived_severity(thresholdCrossed) and probable_cause(超过数据库内存使用率低阈值) 在标签描述中。

就是我也想解析xml中tag里面的内容。

谁能帮我解决这个问题?非常感谢!

最佳答案

一旦您从 description 标签中获得文本,它就与 XML 解析无关。您只需要进行简单的字符串解析,即可将 type = quality_of_service 键/值字符串转换为更适合在 Python 中使用的内容,例如字典

借助 ElementTree 进行了一些稍微简单的解析, 它看起来像这样

messages = """
<alarm-dictionary source="DDD" type="ProxyComponent">

<alarm code="402" severity="Alarm" name="DDM_Alarm_402">
<message>Database memory usage low threshold crossed</message>
<description>dnKinds = database
type = quality_of_service
perceived_severity = minor
probable_cause = thresholdCrossed
additional_text = Database memory usage low threshold crossed
</description>
</alarm>

...
</alarm-dictionary>
"""

import xml.etree.ElementTree as ET

# Parse XML
tree = ET.fromstring(messages)

for alarm in tree.getchildren():
# Get code and severity
print alarm.get("code")
print alarm.get("severity")

# Grab description text
descr = alarm.find("description").text

# Parse "thing=other" into dict like {'thing': 'other'}
info = {}
for dl in descr.splitlines():
if len(dl.strip()) > 0:
key, _, value = dl.partition("=")
info[key.strip()] = value.strip()
print info

关于python - 在python中解析一个特殊的xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13306457/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com