0

I have a xml file structured like so:

<fcd-export xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/fcd_file.xsd">
<timestep time="28800.00">
</timestep>
<timestep time="28801.00">
    <vehicle id="301614_485_0" x="13944.12" y="13808.84" angle="276.89" type="pkw" speed="0.00" pos="4.40" lane="23914010#0_0" slope="0.00"/>
</timestep>
<timestep time="28802.00">
    <vehicle id="301614_485_0" x="13942.45" y="13809.04" angle="276.89" type="pkw" speed="2.01" pos="6.41" lane="23914010#0_0" slope="0.00"/>
</timestep>
<timestep time="28803.00">
    <vehicle id="302675_485_0" x="14013.72" y="12670.03" angle="172.02" type="pkw" speed="0.00" pos="4.40" lane="51827455#5_0" slope="0.00"/>
    <vehicle id="301614_485_0" x="13939.51" y="13809.40" angle="276.89" type="pkw" speed="3.55" pos="9.96" lane="23914010#0_0" slope="0.00"/>
</timestep> ...

I have to create timestep objects with a list of vehicles inside:

traces = [] # list with traces
tree = ET.parse(traceFile)
root = tree.getroot()
timeID = 0
for second in root.iter('timestep'):
    traces.append(Time(float(second.get('time'))))
    if not second:
        print("timestep empty")
    else:
        for car in second.iter('vehicle'):
            traces[timeID].cars.append(Car(car.get('id'), float(car.get('x')), float(car.get('y'))))
    timeID += 1
return traces

But that isn't working. After creating a timestep object, it's iterating through the whole file, instead of just the vehicles inside it.

Time and Car are classes I created

class Car:
def __init__(self, id, x, y):
    self.id = id
    self.x = x
    self.y = y

class Time:
def __init__(self, sec):
    self.sec = sec
cars = []
def countCars(self):
    return len(self.cars)
9
  • What's your desired output given your sample xml? Commented Mar 13, 2020 at 0:19
  • I need, among other things, to count how many cars are present in each timestep Commented Mar 13, 2020 at 0:24
  • You say that it's iterating through the whole file - can you post your traces output showing this? Ideally for a cut down version of your file (such as the one in your question). Commented Mar 13, 2020 at 0:25
  • 1
    print (Time.cars): [<__main__.Car object at 0x116cf0e50>, <__main__.Car object at 0x116cf0d30>, <__main__.Car object at 0x116cf0b50>, <__main__.Car object at 0x116cf0bb0>] Equal for all 4 Time objects Commented Mar 13, 2020 at 0:37
  • 1
    " count how many cars are present in each timestep" - so, focusing on this, in your xml, the output would be 0, 1, 1, 2? Commented Mar 13, 2020 at 1:01

1 Answer 1

1

Your issue is that your cars arttribute of your Time class is connected to the class itself, rather than an instance of the class. As such, all of your Time classes share the same cars list, and append to and present this same list.

If you move your cars = [] instantiation inside your __init__() function, and change it to self.cars = [], it should resolve your issue.

class Time:
    def __init__(self, sec):
        self.sec = sec
        self.cars = []

    def countCars(self):
        return len(self.cars)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.