Python write to text file

Hello

How can I print the output of an element to a text file using xml.etree.elementtree from https://docs.python.org/2/library/xml.etree.elementtree.html

Please look at the first FOR loop section. In the example below I want to print or pipe child.tag and child.attrib to separate files.

>>> for child in root:
...     print child.tag, child.attrib

Open in new window

MontyAsked:
Who is Participating?
 
Flabio GatesCommented:
perhaps this
with open('tags.txt', 'w') as ftags, open('attribs.txt', 'w') as fattribs:
    for child in root:
        ftags.write('%s\n' % (child.tag,))
        fattribs.write('%s\n' % (child.attrib,))

Open in new window

you may want to read the docs.
0
 
Flabio GatesCommented:
Question inactive
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.