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, 2019 in Python by Fata
• 1,040 points
114 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, 2019 by Wajiha
• 1,930 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,740 points
164 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, 2019 in Python by Omkar
• 68,860 points
95 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, 2019 in Python by Nymeria
• 3,520 points
48 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, 2019 in Python by Nymeria
• 3,520 points
311 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, 2019 in Python by Shabnam
• 920 points
79 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, 2019 in Python by Waseem
• 4,520 points
49 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, 2019 in Java by developer_1
• 3,300 points
876 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, 2019 in Python by Wajiha
• 1,930 points
35 views
0 votes
1 answer

What is timedelta in python?

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

answered Aug 7, 2019 in Python by Wajiha
• 1,930 points
47 views