Appending multiple elements with etree, how to write each element on new line?
<p>I am adding some elements to some nodes in a a graphml file using Python and etree. I have two lists of strings with some data which I want to write to my .graphml file. I have managed to do this but when using the .append() function it writes the two new elements on the same line. Is there a good way to get a line separation between these new elements while writing them in the same loop?</p>

<p>I have the following dataset:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;graphml xmlns="http://graphml.graphdrawing.org/xmlns"&gt;
  &lt;node id="node1"&gt;
    &lt;data key="label"&gt;node1&lt;/data&gt;
    &lt;data key="degree"&gt;6&lt;/data&gt;
  &lt;/node&gt;
  &lt;node id="node2"&gt;
    &lt;data key="label"&gt;node2&lt;/data&gt;
    &lt;data key="degree"&gt;32&lt;/data&gt;
  &lt;/node&gt;
  &lt;node id="node3"&gt;
    &lt;data key="label"&gt;node3&lt;/data&gt;
    &lt;data key="degree"&gt;25&lt;/data&gt;
  &lt;/node&gt;
&lt;/graphml&gt;
</code></pre>

<p>and two lists containing years:</p>

<pre><code>lastActive["2013","2014","2015"]
lastRelated["2012","2014","2011"]
</code></pre>

<p>Using the following code to append the lists as elements in the dataset:</p>

<pre><code>for node in root:

    #checks if correct node
    for index, i in enumerate(nameOfNode):
        if i == node[0].text:

            #create and add lastRelated element
            lastRelated = Element('data')
            lastRelated.set('key', 'lastRelated')
            node.append(lastRelated)
            lastRelated.text = lastRelated[index]

            #create and add lastActive element
            lastActive = Element('data')
            lastActive.set('key', 'lastActive')
            node.append(lastActive)
            lastActive.text = lastActive[index]

            updatedText = etree.tostring(node)

            #write to file
            file = open('dataset.graphml', 'wb')
            file.write(updatedText)
            file.close()
</code></pre>

<p>The following results are:</p>

<pre><code>  &lt;node id="node1"&gt;
  &lt;data key="label"&gt;node1&lt;/data&gt;
  &lt;data key="degree"&gt;6&lt;/data&gt;
  &lt;data key="lastActive"&gt;2015&lt;/data&gt;&lt;data key="lastRelated"&gt;2011&lt;/data&gt;&lt;/node&gt;
</code></pre>

<p>I would like it to be structured as:</p>

<pre><code>  &lt;node id="node1"&gt;
  &lt;data key="label"&gt;node1&lt;/data&gt;
  &lt;data key="degree"&gt;6&lt;/data&gt;
  &lt;data key="lastActive"&gt;2015&lt;/data&gt;
  &lt;data key="lastRelated"&gt;2011&lt;/data&gt;
  &lt;/node&gt;
</code></pre>

<p>Anyone got a solution for this?</p>

<p>You should be able to get the wanted output by providing a suitable value for the <a href="https://docs.python.org/2.7/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.tail" rel="nofollow"><code>tail</code></a> property on the new elements. The <code>tail</code> is text that comes after an element's end tag and before the following element's start tag.</p>

<pre><code>...

thetail = "\n  "
lastRelated.tail = thetail
lastActive.tail = thetail

updatedText = etree.tostring(node)

...
</code></pre>

python xml elementtree graphml