Python write to text file


How can I print the output of an element to a text file using xml.etree.elementtree from

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

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.
Flabio GatesCommented:
Question inactive
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.