-3
如何修改下面的XML片段與Python腳本修改XML文件
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
<vType id="car1_73" length="4.70" minGap="1.00" maxSpeed="12.76" probability="0.00" vClass="passenger" guiShape="passenger/van">
<carFollowing-Krauss accel="2.40" decel="4.00" sigma="0.55"/>
</vType>
<vehicle id="0" type="vTypeDist" depart="0.00" departLane="best" departPos="random" departSpeed="random">
<routeDistribution last="1">
<route cost="108.41" probability="0.44076116" edges="bottom7to7/0 7/0to6/0 6/0to6/1 6/1to5/1 5/1to5/2 5/2to6/2"/>
<route cost="76.56" probability="0.55923884" edges="bottom7to7/0 7/0to6/0 6/0to5/0 5/0to5/1 5/1to5/2 5/2to6/2"/>
</routeDistribution>
</vehicle>
</routes>
使得得到一個看起來是這樣的:
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
<vehicle id="0" type="vTypeDist" depart="0.00" departLane="best" departPos="random" departSpeed="random">
<route edges="bottom7to7/0 7/0to6/0 6/0to5/0 5/0to5/1 5/1to5/2 5/2to6/2"/>
</vehicle>
</routes>
基本上下面已經做
- 完全刪除
<vtype>
(和<carFollowing...>
元素), - 刪除
<routeDistribution...>
, - 創建
<route>
元素只能保存最後edges
屬性從<routeDistribution...>
元素中。
編輯:在這裏,我提供使用xml.etree.ElementTree
我的版本。爲什麼所有的downvotes雖然...:/
import xml.etree.ElementTree as ET
if __name__ == "__main__":
tree = ET.parse('total-test.xml')
root = tree.getroot()
# remove <carFollowing> subelement from each vType
vTypes = root.findall("vType")
for vType in vTypes:
carFollowings = vType.findall("carFollowing-Krauss")
for carFollowing in carFollowings:
vType.remove(carFollowing)
# remove each <vType> (to remove an element reference to its parent is required)
for element in root:
if element.tag == "vType":
root.remove(element)
# from root get into <vehicle>
vehicles = root.findall("vehicle")
for vehicle in vehicles:
# for each <vehicle> get reference <routeDistribution>s
routeDistributions = vehicle.findall("routeDistribution")
for routeDist in routeDistributions:
# for each vehicle distrbution get reference to <route>s
routes = routeDist.findall("route")
# fill a container with dictionaries which represent <route> attributes
listOfRouteDicts = list()
for route in routes:
listOfRouteDicts.append(route.attrib)
# find the min_cost for the given routes
min_cost = min(float(routeDict['cost']) for routeDict in listOfRouteDicts)
print(min_cost)
for route in routes:
if route.get('cost') == str(min_cost):
# remove the other attributes of the <route>, we only want the <edges>
route.attrib = {routeAttr:v for routeAttr,v in route.attrib.items() if routeAttr == "edges"}
vehicle.append(route) # move route one level-up to <vehicle> because <routeDistribution> needs to be removed
else:
routeDist.remove(route) # remove all routes which don't have the lowest cost
# remove the <routeDistribution> for each <vehicle>
vehicle.remove(routeDist)
vehicle.set('type', 'vTypeDist')
tree.write('output.xml')
你可以有alook這裏:https://wiki.python.org/moin/MiniDom – Stefano
@Stefano我還沒有嘗試過很多,只要我不熟悉Python中與xml相關的東西。因此,即使我認爲你已經做了一些更多的努力,然後只是要求爲你做腳本,我已經在下面發佈了一個「快速和骯髒」的代碼來幫助你入門,所以請打開我的建議 –
。 – Stefano