title: How do I remove \n characters in django html rendering of an R xtable?
body: <p>I have an <code>xtable</code> generated in R that I want to insert into a template in Django. </p>
  
  <p>The django view is file <code>my_view.py</code> is:</p>
  
  <pre><code>def my_view(request):
      mtcars = open('mtcars.html').readlines()
      context = {'mtcars':mtcars}
      return render(request, 'my_view.html', context)
  </code></pre>
  
  <p>The <code>xtable</code> rendered in <code>R</code> appears thusly when using <code>readlines()</code> into python:</p>
  
  <pre><code>['&lt;!-- html table generated in R 3.1.2 by xtable 1.7-4 package --&gt;\n',
   '&lt;!-- Sun Jan 18 13:32:17 2015 --&gt;\n',
   '&lt;table border=1&gt;\n',
   '&lt;tr&gt; &lt;th&gt;  &lt;/th&gt; &lt;th&gt; mpg &lt;/th&gt; &lt;th&gt; cyl &lt;/th&gt; &lt;th&gt; disp &lt;/th&gt; &lt;th&gt; hp &lt;/th&gt; &lt;th&gt; drat &lt;/th&gt; &lt;th&gt; wt &lt;/th&gt; &lt;th&gt; qsec &lt;/th&gt; &lt;th&gt; vs &lt;/th&gt; &lt;th&gt; am &lt;/th&gt; &lt;th&gt; gear &lt;/th&gt; &lt;th&gt; carb &lt;/th&gt;  &lt;/tr&gt;\n',
   '  &lt;tr&gt; &lt;td align="right"&gt; Mazda RX4 &lt;/td&gt; &lt;td align="right"&gt; 21.00 &lt;/td&gt; &lt;td align="right"&gt; 6.00 &lt;/td&gt; &lt;td align="right"&gt; 160.00 &lt;/td&gt; &lt;td align="right"&gt; 110.00 &lt;/td&gt; &lt;td align="right"&gt; 3.90 &lt;/td&gt; &lt;td align="right"&gt; 2.62 &lt;/td&gt; &lt;td align="right"&gt; 16.46 &lt;/td&gt; &lt;td align="right"&gt; 0.00 &lt;/td&gt; &lt;td align="right"&gt; 1.00 &lt;/td&gt; &lt;td align="right"&gt; 4.00 &lt;/td&gt; &lt;td align="right"&gt; 4.00 &lt;/td&gt; &lt;/tr&gt;\n',
   '  &lt;tr&gt; &lt;td align="right"&gt; Mazda RX4 Wag &lt;/td&gt; &lt;td align="right"&gt; 21.00 &lt;/td&gt; &lt;td align="right"&gt; 6.00 &lt;/td&gt; &lt;td align="right"&gt; 160.00 &lt;/td&gt; &lt;td align="right"&gt; 110.00 &lt;/td&gt; &lt;td align="right"&gt; 3.90 &lt;/td&gt; &lt;td align="right"&gt; 2.88 &lt;/td&gt; &lt;td align="right"&gt; 17.02 &lt;/td&gt; &lt;td align="right"&gt; 0.00 &lt;/td&gt; &lt;td align="right"&gt; 1.00 &lt;/td&gt; &lt;td align="right"&gt; 4.00 &lt;/td&gt; &lt;td align="right"&gt; 4.00 &lt;/td&gt; &lt;/tr&gt;\n',
  </code></pre>
  
  <p>The template file <code>my_view.html</code> is:</p>
  
  <pre><code>{% extends 'base.html' %}
  
  {% block content %}
      {{mtcars|safe}}
  {% endblock %}
  </code></pre>
  
  <p>The problem is that the <code>safe</code> rendering takes all the lines in the file and shows <code>\n</code> characters as shown here:</p>
  
  <p><img src="https://i.stack.imgur.com/nhqKQ.png" alt="enter image description here"></p>
  
  <p>I've tried working around this by replacing the <code>\n</code> in Python, but I end up with the empty list rows <code>''</code>.  If I <code>.read()</code> the file the whole thing gets rendered as a comment (the first row has <code>&lt;!--</code>).</p>
  
  <blockquote>
    <p>Is there a way to control the django <code>safe</code> rendering of the html table to remove the <code>\n</code> or is there a better way to insert the html table?  </p>
  </blockquote>
  
  <p><strong>Note:</strong> the html table is generated dynamically, otherwise I'd embed it directly (it is rendered appropriately when I embed the text in the template).</p>