python - print a csv row into a HTML column
<p>I'm trying to print the data of a CSV column into an HTML table</p>

<p>CSV file is like this (sample) </p>

<pre><code>firstname, surname
firstname, surname
firstname, surname
firstname, surname
firstname, surname
firstname, surname
firstname, surname
</code></pre>

<p>I can read this data in ok - and get it to print out into a table via the following:</p>

<pre><code>import csv
import sys
from fpdf import FPDF, HTMLMixin

#load in csv file
data = csv.reader(open(sys.argv[1]))

names = ""

#Read column names from first line of the file
fields = data.next()
for row in data:
    names = row[0] + " " + row[1]
    html_row = " &lt;tr&gt; "
    html_col = " &lt;td border=0 width=15%&gt;" + names + "&lt;/td&gt;&lt;/tr&gt;"
    html_out = html_out + html_row + html_col 


html = html_header + html_out + html_footer
print html
pdf.write_html(html)
pdf.output('test2.pdf', 'F')
</code></pre>

<p>this gives the following:</p>

<pre><code>&lt;tr&gt;&lt;td border=0 width=15%&gt;firstname surname&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td border=0 width=15%&gt;firstname surname&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td border=0 width=15%&gt;firstname surname&lt;/td&gt;&lt;/tr&gt;
</code></pre>

<p>ie - every name is on a separate row - what i'd like to do is have every name as a cell column cell </p>

<pre><code>&lt;tr&gt;&lt;td border=0 width=15%&gt;firstname surname&lt;/td&gt;&lt;td border=0 width=15%&gt;firstname surname&lt;/td&gt;&lt;td border=0 width=15%&gt;firstname surname&lt;/td&gt;&lt;/tr&gt;
</code></pre>

<p>thanks</p>

<p>You only need one <code>&lt;tr&gt;</code> to make a single row.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-css lang-css prettyprint-override"><code>table, td {
  border: solid 1px #CCC;
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table&gt;
    &lt;tr&gt;
        &lt;td&gt;firstname surname&lt;/td&gt;
        &lt;td&gt;firstname surname&lt;/td&gt;
        &lt;td&gt;firstname surname&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;</code></pre>
</div>
</div>
</p>

<p>In order to make this work in your code, you need to create you row outside the loop:</p>

<pre><code>html_row = '&lt;tr&gt;' # open row

for row in data:
    names = row[0] + " " + row[1]
    # append columns to the row
    html_row += "&lt;td border=0 width=15%&gt;" + names + "&lt;/td&gt;"

html_row += '&lt;/tr&gt;' # close row

html_out = html_row
</code></pre>

python html