0

I have an xml file as below and I want to read data from my device and write the data into "value" column on certain name by using python script.

<TopoDefs>   
<Device Name="SNB" LogicalType="CORE" IdCode="0x03886A21" Mask="0xf0000000">
<Variable name="A0" value="0x52001321" type="GREEN" />
<Variable name="A1" value="0x51001321" type="GREEN" />
<Variable name="A2" value="0x03001321" type="GREEN" />
<Variable name="A3" value="0x14001321" type="GREEN" />
<Variable name="B0" value="0x5A001321" type="BLUE" />
<Variable name="B1" value="0x51001321" type="BLUE" />
<Variable name="B2" value="0x04001321" type="BLUE" />
<Variable name="B3" value="0x05001321" type="BLUE" />
</Device>
</TopoDefs>

May I asks how to I point to the variable A0 to write a new data on "value" attribute? What is the differences among find, findall, iterfind, itertext?

1 Answer 1

1

Using lxml , that I argue is the best tool available for the task:

from lxml import etree as ET


>>> root = ET.fromstring('<TopoDefs><Device Name="SNB" LogicalType="CORE" IdCode="0x03886A21" Mask="0xf0000000"><Variable name="A0" value="0x52001321" type="GREEN" /><Variable name="A1" value="0x51001321" type="GREEN" /><Variable name="A2" value="0x03001321" type="GREEN" /><Variable name="A3" value="0x14001321" type="GREEN" /><Variable name="B0" value="0x5A001321" type="BLUE" /><Variable name="B1" value="0x51001321" type="BLUE" /><Variable name="B2" value="0x04001321" type="BLUE" /><Variable name="B3" value="0x05001321" type="BLUE" /></Device></TopoDefs>')
>>> root.xpath('.//Variable[@name="A0"]')[0].attrib['value']='test'
>>> print ET.tostring(root,pretty_print=True)
<TopoDefs>
  <Device Name="SNB" LogicalType="CORE" IdCode="0x03886A21" Mask="0xf0000000">
    <Variable name="A0" value="test" type="GREEN"/>
    <Variable name="A1" value="0x51001321" type="GREEN"/>
    <Variable name="A2" value="0x03001321" type="GREEN"/>
    <Variable name="A3" value="0x14001321" type="GREEN"/>
    <Variable name="B0" value="0x5A001321" type="BLUE"/>
    <Variable name="B1" value="0x51001321" type="BLUE"/>
    <Variable name="B2" value="0x04001321" type="BLUE"/>
    <Variable name="B3" value="0x05001321" type="BLUE"/>
  </Device>
</TopoDefs>

Or you can loop through Variables and change all values as I understood you wanted:

>>> for i in root.xpath('.//Variable'):
...     i.attrib['value']='CHANGE'
...     
>>> print ET.tostring(root,pretty_print=True)
<TopoDefs>
  <Device Name="SNB" LogicalType="CORE" IdCode="0x03886A21" Mask="0xf0000000">
    <Variable name="A0" value="CHANGE" type="GREEN"/>
    <Variable name="A1" value="CHANGE" type="GREEN"/>
    <Variable name="A2" value="CHANGE" type="GREEN"/>
    <Variable name="A3" value="CHANGE" type="GREEN"/>
    <Variable name="B0" value="CHANGE" type="BLUE"/>
    <Variable name="B1" value="CHANGE" type="BLUE"/>
    <Variable name="B2" value="CHANGE" type="BLUE"/>
    <Variable name="B3" value="CHANGE" type="BLUE"/>
  </Device>
</TopoDefs>
Sign up to request clarification or add additional context in comments.

2 Comments

sorry im new in python, may I know how to I install lxml library in Windows?
lxml.de/installation.html , this should guide you through the process. there are binary packages available for windows, i suggest you choose one of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.