I am a beginner and trying to parse some data from .xml files which has a structure as below.
<parking id="pucpr">
<space id="1" occupied="0">
<rotatedRect>
<center x="300" y="207" />
<size w="55" h="32" />
<angle d="-74" />
</rotatedRect>
<contour>
<point x="278" y="230" />
<point x="290" y="186" />
<point x="324" y="185" />
<point x="308" y="230" />
</contour>
</space>
<space id="2" occupied="0">
<rotatedRect>
<center x="332" y="209" />
<size w="56" h="33" />
<angle d="-77" />
</rotatedRect>
<contour>
<point x="325" y="185" />
<point x="355" y="185" />
<point x="344" y="233" />
<point x="310" y="233" />
</contour>
</space>
.
.
.
</parking>
There are hundreds of such files in different folders. I wrote the code below to parse data from all of those .xml files.
import xml.etree.ElementTree as ET
import os
import xlsxwriter
data_path = '/Users/jaehyunlee/Desktop/for_test'
# Read full directory and file name in the folder
for path, dirs, files in os.walk(data_path):
for file in files:
if os.path.splitext(file)[1].lower() == '.xml': # filtering only for .xml files
full_path = os.path.join(path, file)
# Parsing data from .xml file
tree = ET.parse(full_path)
root = tree.getroot()
for space in root.iter('space'):
car = space.attrib["occupied"]
car_int = int(car)
The problem occurs when I try to parse the value of attribute 'occupied'. When I run the code, it returns KeyError: 'occupied'. For other attributes, such as 'x', 'y', 'w', 'h', it works perfectly fine. Could someone help?
p.s. When I convert one .xml file individually, this error does not occur. But it happens when I try to iterate for all files in the folder.
print(space)andprint(space.attrib)<space>withoutoccupied. Maybe you should checkif "occupied" in space.attrib:or usespace.attrib.get("occupied", default_value)to get default value if there is no"occupied"