当使用.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
你可以用 itertext()
itertext()
for string in root.findall('string'): value = ''.join(string.itertext()) print(value) # output: # value1 value2. value3