代码之家  ›  专栏  ›  技术社区  ›  Gorthez

将XML解析为带有<b>标签的字符串[重复]

  •  0
  • Gorthez  · 技术社区  · 12 月前

    当使用.findall('string')时,我只得到value1,其余部分被忽略。如何获得全部价值? xml文件输入:

    <resources>
     <string name="key">value1 <b>value2</b>. value3</string>
    </resources>
    

    python代码:

    import xml.etree.ElementTree as ET
    tree = ET.parse(xml_file)
    root = tree.getroot()
    for string in root.findall('string'):
           name = string.get('name')
           value = string.text
    
    1 回复  |  直到 12 月前
        1
  •  1
  •   Guy    12 月前

    你可以用 itertext()

    for string in root.findall('string'):
        value = ''.join(string.itertext())
        print(value)
    
    # output:
    # value1 value2. value3