1

Lets say I have TARGET_FILE.xml like below

After updating value of Member and writing it to same file using

TARGET_FILE_TREE.write(TARGET_FILE)

The overwritten file has no commented line as in original file, could you please help in tacking this issue

This is my main code:

for node in NvItemData_Nodes: 
    if node.attrib.get("id") == "5153": 
        print "yes" 
        print node.find('Member').text 
        node.find('Member').text = SVN 
        print node.find('Member').text 
TARGET_FILE_TREE.write(TARGET_FILE)
1
  • for node in NvItemData_Nodes: if node.attrib.get("id") == "5153": print "yes" print node.find('Member').text node.find('Member').text = SVN print node.find('Member').text TARGET_FILE_TREE.write(TARGET_FILE) Is my main code Commented Dec 28, 2020 at 10:54

1 Answer 1

1

When reading the xml-file, the data is converted to a python-data strucute. Because the comments are just comments they are discarded and not saved in this data structure. (That is what comments are for.)

When saving, the python-data-structure you edited is converted back to an xml format and the previous data is overwritten. As the comment is not part of the data structure, it is not written to the fil again.

In Python 3.8 you can preserve comments like this https://stackoverflow.com/a/59561426/8106583

For Python 2.7 I found this solution to preserve comments: https://stackoverflow.com/a/34324359/8106583

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

1 Comment

@SHASHIBHUSHAN GOTUR Please accept the answer if it worked for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.