1

Im trying to implement a script python that update a specific value of the xml parameters. my issue that the parameter that I want to update appears multiple times respectively one row after the other. like this (the paramter called /DATA:

this is my xml file below which always starts with CATALOG And then CD and then DATA paramter:

<CATALOG>
  <CD>
    <DATA>1</DATA>
    <DATA>2</DATA>
    <DATA>3</DATA>
    <DATA>6</DATA>
    <DATA>7</DATA>
    <DATA>9</DATA>
  </CD> 
</CATALOG> 

Whenever I try by my script python to update that parameter I see all other parameters also changed to same value and I dont want the others paramters within the same name to be changed.

like each time I try to run my script python it changes all the data like this .. for instance I want to update first 4 rows of <DATA></DATA> as number 20 21 22 23 and others DATA parameters keep them with defaulted value which means it shall be as this:

<CATALOG>
  <CD>
    <DATA>20</DATA>
    <DATA>21</DATA>
    <DATA>22</DATA>
    <DATA>23</DATA>
    <DATA>7</DATA>
    <DATA>9</DATA>
  </CD> 
</CATALOG>

but when I run my script python actually it changes all to number 20 and the output is like this:

<CATALOG>
  <CD>
    <DATA>20</DATA>
    <DATA>20</DATA>
    <DATA>20</DATA>
    <DATA>20</DATA>
    <DATA>20</DATA>
    <DATA>20</DATA>
  </CD> 
</CATALOG>

Any help please how could I implement a function in python 2.7 to change the value of DATA parameter at each row of <DATA></DATA> in the xml file separately without affecting the other rows of DATA that has same parameter name?

much appreciated !

Note the xml file has default parameters that Im trying to modify by script python new values for the parameter DATA

3
  • I deleted it actually because the issue here the name of the parameter DATA is same for all others row, so my issue primarily is how to differentiate between DATA parameters that has same name ? Commented Jan 31, 2022 at 8:17
  • A bit of advice: it is expected that "all" questions on SO regarding an issue on some code have said code pasted in them (albeit without actually irrelevant/private parts). Even if you believe it's pointless, that's for the users who are willing to answer to decide. Maybe your code only needs one or two lines to be fixed, or maybe the whole code needs to be rewritten. But deleting your code forces everyone to rewrite it from scratch, and not only is that undesirable, but it will also be harder for you to know what was wrong in your code and learn from your mistakes. Commented Jan 31, 2022 at 11:43
  • Please provide enough code so others can better understand or reproduce the problem. Commented Feb 9, 2022 at 3:59

1 Answer 1

0
from xml.etree import ElementTree as ET

original_file_content = """\
<CATALOG>
  <CD>
    <DATA>1</DATA>
    <DATA>2</DATA>
    <DATA>3</DATA>
    <DATA>6</DATA>
    <DATA>7</DATA>
    <DATA>9</DATA>
  </CD> 
</CATALOG> 
"""
data_values_to_replace = [20, 21, 22, 23]

root = ET.fromstring(original_file_content)

for data_node, value_to_replace in zip(root.findall(".//DATA"), data_values_to_replace):
    data_node.text = str(value_to_replace)

# now get the result XML
result = ET.tostring(root, encoding="unicode")
print(result)  # or do something else : ET.ElementTree(root).write("savefile_name.xml")

expected_final_content = """\
<CATALOG>
  <CD>
    <DATA>20</DATA>
    <DATA>21</DATA>
    <DATA>22</DATA>
    <DATA>23</DATA>
    <DATA>7</DATA>
    <DATA>9</DATA>
  </CD> 
</CATALOG>\
"""
assert result == expected_final_content
Sign up to request clarification or add additional context in comments.

Comments

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.