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,050 points
1,747 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,950 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,710 points
922 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
• 69,210 points
683 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,560 points
409 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,560 points
2,251 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 7, 2019 in Java by developer_1
• 3,310 points
3,801 views
0 votes
0 answers

Convert XML String to Object

I'm getting XML strings over a socket ...READ MORE

Jun 11 in C# by krishna
• 2,820 points
360 views
0 votes
0 answers

Parsing HTML using Python

I'm seeking a Python HTML Parser package ...READ MORE

Jul 12 in HTML by Ashwini
• 2,760 points
64 views
0 votes
0 answers

Best XML Parser for PHP

I've used the XML Parser before, and ...READ MORE

Jul 29 in PHP by Kithuzzz
• 20,460 points
97 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,950 points
1,037 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,950 points
257 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP