简体   繁体   中英

Modifying xml attributes through python

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.

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

Assuming that child is the <Traces/> node.

Edit:
0 needs to be a string

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM