-1

I have the following XML file in which the following information is present.

<PHYSICAL_TLINE>
    <Traces general_diff="0" z_array="0" s_array="0" w_array="0" etch_factor="0.35" TS_track2track="0" TS_DQS="0" TW_DQS="0" TS_byte2dqs="0" TS_byte2byte="0" TS_DQ="0" TW_DQ="0" dsl_offset="0" D="20" TS="7" TW="5"/>
<PHYSICAL_TLINE>

Is there a way to set the values of these elements through python? For example, if I want to change the value of s_array to 5 instead of 0?. I know that there is the xml.etree set command but I'm not too sure on how to set the values of these attributes in the child through python.

3
  • Did you even attempt to google "python xml" ? Commented Apr 25, 2018 at 14:25
  • Of course I did. Commented Apr 25, 2018 at 14:27
  • Possible duplicate of stackoverflow.com/questions/17922056/… Commented Apr 25, 2018 at 14:28

2 Answers 2

2
child.attrib["s_array"] = '0'

Assuming that child is the <Traces/> node.

Edit:
0 needs to be a string

Sign up to request clarification or add additional context in comments.

1 Comment

0 should be a string, child.attrib["s_array"] = '0'
1

This documentation may be helpful for you: https://docs.python.org/2/library/xml.etree.elementtree.html

Note 19.7.1.4. Modifying an XML File

Modifying some code like this should acheive the desired result:

for rank in root.iter('rank')
    rank.set('updated', 'yes')
tree.write('output.xml')

2 Comments

I tried this but I am getting the error AttributeError: 'NoneType' object has no attribute 'set'
Please provide code based off of the xml above and we may be able to help out :).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.