修改Python中的XML
我要做的是更新registration_no = de2115
xml文件 的
<?xml version="1.0"?>
<motorvehicles>
<vehicle>
<registration_no> CBB1456 </registration_no>
<make> Toyota </make>
<model> Premio </model>
</vehicle>
<vehicle>
<registration_no> PR2245 </registration_no>
<make> Mazda </make>
<model> Bongo </model>
</vehicle>
<vehicle>
<registration_no> DE2115 </registration_no>
<make> TATA </make>
<model> Sumo </model>
</vehicle>
<vehicle>
<registration_no> CAR7785 </registration_no>
<make> Kia </make>
<model> Optima </model>
</vehicle>
</motorvehicles>
品牌和模型的值 我尝试了以下代码,但它不起作用。
for elm in root.findall("./vehicle/[@registraion_no='DE2115']/make"):
elm.text="Nissan"
What I want do do is update the values of make and model where registration_no = DE2115
XML File
<?xml version="1.0"?>
<motorvehicles>
<vehicle>
<registration_no> CBB1456 </registration_no>
<make> Toyota </make>
<model> Premio </model>
</vehicle>
<vehicle>
<registration_no> PR2245 </registration_no>
<make> Mazda </make>
<model> Bongo </model>
</vehicle>
<vehicle>
<registration_no> DE2115 </registration_no>
<make> TATA </make>
<model> Sumo </model>
</vehicle>
<vehicle>
<registration_no> CAR7785 </registration_no>
<make> Kia </make>
<model> Optima </model>
</vehicle>
</motorvehicles>
I tried the following Code But it doesn't work.
for elm in root.findall("./vehicle/[@registraion_no='DE2115']/make"):
elm.text="Nissan"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你快到了。您可以简单地做到这一点 - 假设只有一辆具有目标注册号的车辆:
You're almost there. You can do it simply - assuming there's only one vehicle with the target registration number: