Writing output to new file using xml.etree.ElementTree in python

0 votes
I want to write my xml output to a new file instead of making changes to the original file. What should I do? I have been using xml.etree.ElementTree module
Jul 24 in Python by Fata
• 1,040 points
26 views

1 answer to this question.

0 votes
To write results to a new file, you can use the write() function as follows:

EXAMPLE:

import xml.etree.ElementTree as ET

for description in myroot.iter('description'):

     new_desc = str(description.text)+'new text added'

     description.text = str(new_desc)

     description.set('updated', 'yes')

mytree.write('new.xml')

Here, I have added some more text and an attribute to a tag named description that is already present in my file and writing the output to a new file.
answered Jul 25 by Wajiha
• 1,920 points

Related Questions In Python

0 votes
1 answer

How to correctly return an a dictionary as an output in zappier code using python?

David here, from the Zapier Platform team. ...READ MORE

answered Dec 3, 2018 in Python by charlie_brown
• 7,720 points
126 views
0 votes
1 answer

How to check latest change time of each file using glob in python?

You can get the changing time from ...READ MORE

answered Jan 2 in Python by Omkar
• 67,600 points
67 views
0 votes
1 answer

How to get textual output when using exceptions in Python?

Hi, the answer is pretty simple.  Without the ...READ MORE

answered Jan 17 in Python by Nymeria
• 3,520 points
38 views
0 votes
1 answer

How to output the rows which are affected using SQLAlchemy in Python?

Hi, good question. This is actually not ...READ MORE

answered Feb 15 in Python by Nymeria
• 3,520 points
150 views
+1 vote
1 answer

How to check if a string is in a file, using python?

Hi, Try the below given code: with open('myfile.txt') as ...READ MORE

answered Jul 4 in Python by Shabnam
• 920 points
43 views
0 votes
1 answer
0 votes
0 answers

How to handle large files using file handling in python?

I have used file handling for smaller ...READ MORE

Aug 2 in Python by Waseem
• 4,480 points
20 views
0 votes
1 answer

How to parse invalid XML in Java ?

That "XML" is worse than invalid – it's not well-formed; ...READ MORE

answered Mar 6 in Java by developer_1
• 3,300 points
525 views
0 votes
1 answer

Slicing a list using a variable, in Python

Yes its possible. Use the following piece ...READ MORE

answered Jul 2 in Python by Wajiha
• 1,920 points
24 views
0 votes
1 answer

What is timedelta in python?

It represents a duration which is basically ...READ MORE

answered Aug 7 in Python by Wajiha
• 1,920 points
28 views