如何使用python提取xml中的单个特定标记值并修改
我想阅读IntakeFanSpeedMax、IntakeFanSpeedMin标记值并在下面的xml中使用python进行更新,尝试了许多教程,但无法阅读和更新它。请在这方面帮助我,提前谢谢如何使用python提取xml中的单个特定标记值并修改,python,xml,Python,Xml,我想阅读IntakeFanSpeedMax、IntakeFanSpeedMin标记值并在下面的xml中使用python进行更新,尝试了许多教程,但无法阅读和更新它。请在这方面帮助我,提前谢谢 <?xml version="1.0"?> <RobotManager Checksum="CCA4FBB2C307EBE537BDCEAFB340F5F22ABE178951B0F7FDDFBDC8939D0A3CAC&quo
<?xml version="1.0"?>
<RobotManager Checksum="CCA4FBB2C307EBE537BDCEAFB340F5F22ABE178951B0F7FDDFBDC8939D0A3CAC">
<Setup>
<AddressHostName>192.168.2.1</AddressHostName>
<PowerStateMonitor>
<PowerStateMonitor1>
<CommPort>2</CommPort>
</PowerStateMonitor1>
<PowerStateMonitor2>
<Pmu>
<!-- in seconds (minimum: 90; maximum: 150) -->
<AcNotPresentShutdownTime>120</AcNotPresentShutdownTime>
<!-- in degrees Celsius (minimum: 0; maximum: 100) -->
<BatteryTemperatureDifferential>6</BatteryTemperatureDifferential>
<!-- in RPMs (minimum: 3000; maximum: 5000) -->
<IntakeFanSpeedMax>4000</IntakeFanSpeedMax>
<!-- in RPMs (minimum: 750; maximum: 1300) -->
<IntakeFanSpeedMin>1000</IntakeFanSpeedMin>
</Pmu>
</RobotManager>
my code:
import xml.etree.ElementTree as ET
fileXml = "C://Users//...//Documents//RobotManagerSettings.xml"
tree = ET.parse(fileXml)
root = tree.getroot()
IntakeFanSpeedMax = root.find('IntakeFanSpeedMax').text
print(IntakeFanSpeedMax)
getting below error:
"C:\Users\Sagar Hosamath\AppData\Local\Programs\Python\Python39\python.exe" "C:/Users/Sagar Hosamath/PycharmProjects/Coord/venv/Scripts/xmlPath.py"
Traceback (most recent call last):
File "C:\Users\Sagar Hosamath\PycharmProjects\Coord\venv\Scripts\xmlPath.py", line 6, in <module>
IntakeFanSpeedMax = root.find('IntakeFanSpeedMax').text
AttributeError: 'NoneType' object has no attribute 'text'
Process finished with exit code 1
192.168.2.1
2.
120
6.
4000
1000
我的代码:
将xml.etree.ElementTree作为ET导入
fileXml=“C://Users//……///Documents//RobotManagerSettings.xml”
tree=ET.parse(fileXml)
root=tree.getroot()
IntakeFanSpeedMax=root.find('IntakeFanSpeedMax').text
打印(凹版风扇SpeedMax)
获取以下错误:
“C:\Users\Sagar Hosamath\AppData\Local\Programs\Python\Python39\Python.exe”“C:/Users/Sagar Hosamath/PycharmProjects/Coord/venv/Scripts/xmlPath.py”
回溯(最近一次呼叫最后一次):
文件“C:\Users\Sagar Hosamath\PycharmProjects\Coord\venv\Scripts\xmlPath.py”,第6行,在
IntakeFanSpeedMax=root.find('IntakeFanSpeedMax').text
AttributeError:“非类型”对象没有属性“文本”
进程已完成,退出代码为1
实现这一点的最简单方法是使用pyquery和xml文本,您可以按如下方式应用它(这也将用更新的标记值替换标记值):
从pyquery导入pyquery作为pq
xml_text=“”
192.168.2.1
2.
120
6.
4000
1000
'''
d=pq(xml\U文本)
max_obj=d('进气风扇转速max')
min_obj=d(‘进气速度min’)
打印('进气速度最大:{}'。格式(Max_obj.text())
打印('进气速度Min:{}'。格式(Min_obj.text())
新的最大值='3000'
新的最小值='300'
max_obj.replaceWith({}.format(新的最大值))
min_obj.replaceWith(“{}.”格式(新的_min_值))
印刷品(d)
查找嵌套标记时,需要以下格式
>>> root.findall(".//IntakeFanSpeedMax")
[<Element 'IntakeFanSpeedMax' at 0x000001E40DBE8D60>]
>>> root.findall(".//IntakeFanSpeedMax")[0].text
'4000'
你能展示你迄今为止尝试过的代码,以及它以何种方式不起作用吗?请共享一个有效的XML。谢谢你,它起了作用,我如何设置和保存XML,我按照你的建议尝试了以下代码:import XML.etree.ElementTree as ET fileXml=“C://Users/..//Documents//RobotManagerSettings.XML”tree=ET.parse(fileXml)root=tree.getroot()value=root.findall(“.//IntakeFanSpeedMax”)[0]。如果value=“4000”则为text:root.set(“.//IntakeFanSpeedMax”,8000)树。write('output.xml',encoding='UTF-8',xml\u declaration=True)正在获取以下错误:AttributeError:'str'对象没有属性'write'答案已更新。
>>> root.findall(".//IntakeFanSpeedMax")
[<Element 'IntakeFanSpeedMax' at 0x000001E40DBE8D60>]
>>> root.findall(".//IntakeFanSpeedMax")[0].text
'4000'
import xml.etree.ElementTree as ET
fileXml = "C://Users//..//Documents//RobotManagerSettings.xml"
tree = ET.parse(fileXml)
root = tree.getroot()
value = root.findall(".//IntakeFanSpeedMax")[0].text
if value == "4000":
root.findall(".//IntakeFanSpeedMax")[0].text = "8000"
tree.write('output.xml',encoding='UTF-8',xml_declaration=True)