问题
I have 14 different XML files like this and I'm trying to get ActionUnit Number values (ex. 45 and 5). I need to do it for the other 13 XML files too with a loop. After that, I need to create subfolders for each value that I extracted. I tried a code for value extraction but I get none as a result;
import xml.etree.cElementTree as ET
tree= ET.ElementTree(file=r'C:\Users\LME_s\Desktop\python quiz\Sessions\1\S001-001-oao_aucs.xml')
root= tree.getroot()
for chld in root:
print (chld.get('ActionUnit Number'))
回答1:
Your question is actually few questions... Here is how to extract the data you need from the xml.
import xml.etree.ElementTree as ET
XML = '''<ActionUnitCoding>
<ActionUnit Number="45"></ActionUnit>
<ActionUnit Number="5"></ActionUnit>
</ActionUnitCoding>'''
root = ET.fromstring(XML)
numbers = [e.attrib['Number'] for e in root.findall('.//ActionUnit')]
print(numbers)
output
['45', '5']
来源:https://stackoverflow.com/questions/65276888/extracting-string-from-xml-files-with-loop-in-python