提取XML键的字符串/值

2024-09-25 08:28:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从xml文件中提取字符串,但不使用etree。你知道吗

XML的一小部分:

<key>FDisplayName</key>
<string>Dripo</string>
<key>CFBundleIdentifier</key>
<string>com.getdripo.dripo</string>
<key>DTXcode</key>

假设我想提取com.getdripo.dripo,我怎么能做到这一点,但没有使用etree?你知道吗

我只知道如何使用etree,但在这种情况下我不能使用它。你知道吗

在网上找不到任何东西,有什么想法吗?你知道吗


Tags: 文件key字符串comstring情况xmletree
1条回答
网友
1楼 · 发布于 2024-09-25 08:28:28

使用正则表达式。你知道吗

import re
s = """<key>FDisplayName</key>
<string>Dripo</string>
<key>CFBundleIdentifier</key>
<string>com.getdripo.dripo</string>
<key>DTXcode</key>"""

print re.findall("<string>(.*?)</string>", s)      #finds all content between '<string>' tag
print re.findall("<string>(com.*?)</string>", s)

输出:

['Dripo', 'com.getdripo.dripo']
['com.getdripo.dripo']

注意:强烈建议使用XML解析器。你知道吗

相关问题 更多 >