首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XML设置值时出错

使用XML设置值时出错
EN

Stack Overflow用户
提问于 2022-05-03 13:59:13
回答 1查看 28关注 0票数 0

我试图从XML文件中更改节点的值。

我正在使用XML /Element。

我的代码如下:

代码语言:javascript
复制
import xml.etree.ElementTree as ET
import array

tree = ET.parse('Beckhoff ELM37xx.xml')
root = tree.getroot()

global xdevice

for Descriptions in root.findall('.//Descriptions'):
    xdescriptions = Descriptions.find('Devices')
  
for Devices in root.findall('.//Devices'):
    xdevices = Devices.find('Device')

for Device in root.findall('.//Device'):
    xdevice = Device.find('Name').text
    print(xdevice)

##tree.write('Beckhoff ELM37xx.xml')

使用xdevice,我得到了我需要显示的信息,它是10个元素。

代码语言:javascript
复制
ELM3702-0000 2Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision
ELM3702-0000 2Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision
ELM3704-0000 4Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision
ELM3704-0000 4Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision
ELM3704-0001 4Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision, LEMO
ELM3704-0020 4Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision, calibrated
ELM3704-0020 4Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision, calibrated
ELM3702-0101 2Ch. Ana. Input +/-60V, +/-20mA, TC, RTD, Bridge Measuring (SG), IEPE, 24 bit, high precision, isolated channels
ELM3704-1001 4Ch. Ana. Input +/-10V, TC, 24 bit, high precision
ELM3704-1001 4Ch. Ana. Input +/-10V, TC, 24 bit, high precision

我想了解如何使用set更改第一个值,因为索引不起作用,它为每次迭代返回字母E。我想了解如何将这些值存储到列表或元组中,以便能够修改它(或者直接修改它,但可以随意修改列表的一个值)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-10 06:21:34

它被解决了:

代码语言:javascript
复制
import xml.etree.ElementTree as ET
tree = ET.parse('Beckhoff ELM37xx.xml')
root = tree.getroot()
Devices = root.findall('.//Device')
nametag1 = Devices[0].find('Name')
nametag1.text = 'Blanqui'

for Device in root.findall('.//Device'):
    xdevice = Device.find('Name').text
    print(xdevice)
    
tree.write('Beckhoff ELM37xx.xml')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72100566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档