Pythonl lxml adding linebreak between specific nodes when creating xml file
<p>I am writing an XML file using lxml. I am able to write the entire XML file to one line:</p>

<pre><code>&lt;FIXML&gt;&lt;Batch Total="3"&gt;&lt;Hdr SendTime="2016-09-27T13:32:19-05:00"/&gt;&lt;RepeatingNode Price="0.99" RptID="1"&gt;&lt;Date Dt="2016-09-20"/&gt;&lt;/RepeatingNode&gt;&lt;RepeatingNode Price="2.49" RptID="2"&gt;&lt;Date Dt="2016-09-20"/&gt;&lt;/RepeatingNode&gt;&lt;RepeatingNode Price="0.25" RptID="3"&gt;&lt;Date Dt="2016-09-20"/&gt;&lt;/RepeatingNode&gt;&lt;/Batch&gt;&lt;/FIXML&gt;
</code></pre>

<p>and I am able to pretty_print it using the pretty_print parameter:</p>

<pre><code>&lt;FIXML&gt;
    &lt;Batch Total="3"&gt;
        &lt;Hdr SendTime="2016-09-27T13:32:19-05:00"/&gt;
        &lt;RepeatingNode Price="0.99" RptID="1"&gt;
            &lt;Date Dt="2016-09-20"/&gt;
        &lt;/RepeatingNode&gt;
        &lt;RepeatingNode Price="2.49" RptID="2"&gt;
            &lt;Date Dt="2016-09-20"/&gt;
        &lt;/RepeatingNode&gt;
        &lt;RepeatingNode Price="0.25" RptID="3"&gt;
            &lt;Date Dt="2016-09-20"/&gt;
        &lt;/RepeatingNode&gt;
    &lt;/Batch&gt;
&lt;/FIXML&gt;
</code></pre>

<p>However, I would like to write to my file and add a linebreak only after each ReapeatingNode. I would also like to avoid any indentation. My ideal output file would look like this:</p>

<pre><code>&lt;FIXML&gt;
&lt;Batch Total="3"&gt;
&lt;Hdr SendTime="2016-09-27T13:32:19-05:00"/&gt;
&lt;RepeatingNode Price="0.99" RptID="1"&gt;&lt;Date Dt="2016-09-20"/&gt;&lt;/RepeatingNode&gt;
&lt;RepeatingNode Price="2.49" RptID="2"&gt;&lt;Date Dt="2016-09-20"/&gt;&lt;/RepeatingNode&gt;
&lt;RepeatingNode Price="0.25" RptID="3"&gt;&lt;Date Dt="2016-09-20"/&gt;&lt;/RepeatingNode&gt;
&lt;/Batch&gt;
&lt;/FIXML&gt;
</code></pre>

<p>Below is the framework for my code:</p>

<pre><code>import lxml.etree as et
fixml_node = et.Element("FIXML")
batch_node = et.SubElement(fixml_node, "Batch")
et.SubElement(batch_node, "Hdr")

for row in data:
    repeating_node = et.SubElement(batch_node, "RepeatingNode")
    et.SubElement(repeating_node, "Date")

complete_new_file = et.ElementTree(fixml_node)

complete_new_file.write("output_file")

# or below when pretty-printing
complete_new_file.write("output_file", pretty_print=True)
</code></pre>

<p>Any suggestions how I can achieve my desired output?</p>

<p>My solution is to ignore the pretty_print param and add a text or tail with a linebreak where applicable. New code below:</p>

<pre><code>import lxml.etree as et

fixml_node = et.Element("FIXML")
fixml_node.text = "\n"

batch_node = et.SubElement(fixml_node, "Batch")
batch_node.text = "\n"
batch_node.tail = "\n"

header_node = et.SubElement(batch_node, "Hdr")
header_node.tail = "\n"

for row in data:
    repeating_node = et.SubElement(batch_node, "RepeatingNode")
    repeating_node.tail = "\n"

    et.SubElement(repeating_node, "Date")

complete_new_file = et.ElementTree(fixml_node)

complete_new_file.write("output_file")
</code></pre>

<p>This is an easy way to customize the positioning of linebreaks throughout the file. I'll leave this question open for a day or two to see if anyone has a better solution.</p>

python xml lxml