Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python元素树XML迭代_Python_Xml - Fatal编程技术网

python元素树XML迭代

python元素树XML迭代,python,xml,Python,Xml,我有一个xml文件,其结构如下: <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"> <

我有一个xml文件,其结构如下:

<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> ...
但这是行不通的。创建timestep对象后,它将遍历整个文件,而不仅仅是其中的车辆

时间和汽车是我创造的课程

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)

您的问题是您的
cars
art
Time
类的属性连接到类本身,而不是类的实例。因此,您所有的
Time
课程都共享相同的
cars
列表,并附加到该列表并呈现该列表

如果将
cars=[]
实例化移动到
\uuuu init\uuuu()
函数中,并将其更改为
self.cars=[]
,应该可以解决您的问题

上课时间:
定义初始(自我,秒):
self.sec=秒
self.cars=[]
def countCars(自我):
回程透镜(自驾汽车)

给定示例xml,您希望的输出是什么?除其他外,我需要计算每个时间步中有多少辆车。如果您说它正在遍历整个文件,您可以发布您的
跟踪
输出来显示这一点吗?打印(Time.cars):[,]对于所有4个时间对象都相等“计算每个时间步中有多少辆车”-因此,关注这一点,在xml中,输出将是
0,1,1,2
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)