无法使用 XML Etree 设置值
Trouble setting a value using XML Etree
我正在尝试更改 XML 文件中节点的值。
我正在使用 XML Etree/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。我想了解如何将这些值存储到列表或元组中,以便能够修改它(或直接修改它,但随意修改列表中的一个值)。
已解决:
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')
我正在尝试更改 XML 文件中节点的值。
我正在使用 XML Etree/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。我想了解如何将这些值存储到列表或元组中,以便能够修改它(或直接修改它,但随意修改列表中的一个值)。
已解决:
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')