我正在使用Python(minidom)来解析XML文件并像csv文件一样打印输出:
0.0,0.0,51.3,46.6,
49.9,49.0,51.0,46.6,
36.0,24.0,47.8,42.2,
51.0,46.6,49.3,34.1,
相反,该程序会生成以下内容,跳过“Q”标记,除非它们是各自“事件”标记中的最后一个标记...为什么我的程序无法打印这些标记?
0.0,0.0,,46.6
49.9,49.0,,
36.0,24.0,42.2,
51.0,46.6,,
这是XML源文件:
<?xml version="1.0" encoding="UTF-8"?>
<Games id = "1">
<Game id="1" competition_id="1">
<Event id="0" x="0.0" y="0.0">
<Q id="a" end_x="51.3" />
<Q id="b" end_y="46.6" />
</Event>
<Event id="1" x="49.9" y="49.0">
<Q id="a" end_x="51.0" />
<Q id="b" end_y="46.6" />
<Q id="c" q1="tap" />
</Event>
<Event id="2" x="0.0" y="0.0">
<Q id="a" end_y="47.8" />
<Q id="b" end_x="42.2" />
</Event>
<Event id="3" x="51.0" y="46.6">
<Q id="a" end_y="49.3" />
<Q id="b" end_x="34.1" />
<Q id="c" q1="17.8" />
</Event>
</Game>
这是我的代码:
from xml.dom.minidom import parse
import xml.dom.minidom
DOMTree = parse('myfile.xml');
collection = DOMTree.documentElement;
# Get all events in the collection
events = collection.getElementsByTagName("Event")
# Iterate through events
for event in events:
start_x = event.getAttribute('x');
start_y = event.getAttribute('y');
qualifiers = event.getElementsByTagName('Q');
# Iterate through qualifiers
for qualifier in qualifiers:
end_x= qualifier.getAttribute('end_x');
end_y= qualifier.getAttribute('end_y');
print start_x + ',' + start_y + ',' + end_x + ',' + end_y
答案 0 :(得分:1)
如果在限定符循环中查看for限定符的结构,则循环中的end_x和end_y不会发生任何变化。这意味着代码运行并重新分配end_x和end_y的值。在循环结束时,end_x和end_y是最后一个限定符的值
from xml.dom.minidom import parse
import xml.dom.minidom
DOMTree = parse('myfile.xml');
collection = DOMTree.documentElement;
# Get all events in the collection
events = collection.getElementsByTagName("Event")
# Iterate through events
for event in events:
start_x = event.getAttribute('x');
start_y = event.getAttribute('y');
qualifiers = event.getElementsByTagName('Q');
# Iterate through qualifiers
for qualifier in qualifiers:
if(qualifier.hasAttribute('end_x')):
end_x= qualifier.getAttribute('end_x');
elif(qualifier.hasAttribute('end_y')):
end_y= qualifier.getAttribute('end_y');
print start_x + ',' + start_y + ',' + end_x + ',' + end_y
上面的代码应该做你想要的。我注意到的是,最初的顺序是end_x然后end_y然后顺序改变了end_y end_x,所以在样本正确的格式中,翻转了结束坐标。所以输出是
0.0,0.0,51.3,46.6,
49.9,49.0,51.0,46.6,
36.0,24.0,42.2,47.8,
51.0,46.6,34.1,49.3,
希望这有帮助