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>Some variables in sample are not set, but this is due to reducing of sample to aminimal size I guess, so if you fill in these details, the following should work (suboptimal if more than 7 entries due to the hardcoded 15% width on the html td element.</p>

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

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

# Read column names from first line of the file to ignore them
fields = data.next()
</code></pre>

<p>Below the loop from question has been replaced by a list comprehension.
cell_att holds the given attributes of the table cell elements and it will be interpolated like row[0] and row[1] into the strings making up the html_out list.</p>

<pre><code>cell_att = " border=0 width=15%"
row_data = ["&lt;td%s&gt;%s %s&lt;/td&gt;" % (cell_att, row[0], row[1]) for row in data]
</code></pre>

<p>Here one simply joins all cells and injects into a html table row element:</p>

<pre><code>html_out = "&lt;tr&gt;" + "".join(row_data) + "&lt;/tr&gt;"

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

<p>The other answers also give IMO useful hints esp. on the styling level of the HTML. In case you later decide to digest / transform more than 7 names, the above code might be a good start to create rows with at most 7 cells or to adapt the width attribute value by minor modifications.</p>

python html