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
3,457 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,960 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
1,681 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,220 points
1,610 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
1,011 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
3,405 views
0 votes
1 answer

How to parse invalid XML in Java ?

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

answered Mar 7, 2019 in Java by developer_1
• 3,350 points
5,487 views
0 votes
0 answers

Convert XML String to Object

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

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

Parsing HTML using Python

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

Jul 12, 2022 in HTML by Ashwini
• 5,430 points
452 views
0 votes
0 answers

Best XML Parser for PHP

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

Jul 29, 2022 in PHP by Kithuzzz
• 38,000 points
693 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,960 points
2,079 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,960 points
776 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