首页 > 解决方案 > 有没有办法从另一个字符串中获取一个特定的字符串?

问题描述

假设我有下一个字符串:

string = "<dialog id="105">\xa<state>confirmed</state>\xa</dialog>"

此字符串的长度可能会有所不同。有没有一种方法可以让我只confirmed在 python 中获取字符串?

标签: python

解决方案


我稍微修改了您的 xml 字符串,并能够确认为输出,以下是代码:

    import xml.etree.ElementTree as ET

    string = '''<dialog id="105"><state>confirmed</state></dialog>'''
    root = ET.fromstring(string)
    print(root)
    for child in root:
        if child.tag == 'state':
            print(child.text)

让我知道这个是否奏效。


推荐阅读