修改Python中的XML

发布于 2025-02-09 18:12:05 字数 1095 浏览 0 评论 0原文

我要做的是更新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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2025-02-16 18:12:05

你快到了。您可以简单地做到这一点 - 假设只有一辆具有目标注册号的车辆:

root.find('.//vehicle[registration_no=" DE2115 "]/make').text="Nissan" 

You're almost there. You can do it simply - assuming there's only one vehicle with the target registration number:

root.find('.//vehicle[registration_no=" DE2115 "]/make').text="Nissan" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文