我试图从XML文件中更改节点的值。
我正在使用XML /Element。
我的代码如下:
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个元素。
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。我想了解如何将这些值存储到列表或元组中,以便能够修改它(或者直接修改它,但可以随意修改列表的一个值)。
发布于 2022-05-10 06:21:34
它被解决了:
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')https://stackoverflow.com/questions/72100566
复制相似问题