Translate CSV To HTML

January 15, 2013

A common format for data storage is the CSV format for comma-separated values. A common format for data presentation is HTML for browsers using tables.

Your task is to write a function that reads a file in CSV format and translates it to a table in HTML format. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Doesn’t seem anyone other than Programming Praxis dealt with quoted strings
(which, granted, isn’t necessarily a part of ‘official’ CSV files). It does
make the code quite a bit more interesting if you do. :)

Anyways, here’s my short (ugly) version that doesn’t deal with quoted
strings:

Python 3.3 solution. Uses the standard library csv to read the file and elementtree to build the table as an xml document. The tostring method takes care of properly escaping characters like <, if any, in the file.

import csv
import xml.etree.ElementTree as et

def csv2html(filelike_obj, id_=None):

table = et.Element(‘table’, {‘id':id_} if id_ else {})

for n, line in enumerate(csv.reader(filelike_obj)):
row = et.SubElement(table, ‘tr’)
if n&1: row.set(‘class’, ‘odd’)

1. It generates 1-column table – for every comma in the source you produce a new row in the output. Instead it should produce a cell for every comma and a row for each newline.
2. I see no reason for manual buffered read in this case – you don’t process those chunks one by one, and in the end you still have a full raw string in memory, so why not use something like StreamReader.ReadToEnd() for simplicity?
3. I can’t see a practical reason for using StringBuilder to store the final output here. StringBuilder might save you memory allocations and some processor cycles if you have lots of string operations, true. But you only have 2 appends. Your code will still likely force a StringBuilder to allocate memory for its internal storage, perhaps more times than it would take to allocate 3 usual immutable strings in the first place.