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.
[…] today’s Programming Praxis exercise, our goal is to translate a csv file to an HTML table. Let’s get […]
My Haskell solution (see http://bonsaicode.wordpress.com/2013/01/15/programming-praxis-translate-csv-to-html/ for a version with comments):
[…] Question is from here: […]
Java solution here.
Answer in awk (here)[http://psykotedy.tumblr.com/post/40602728216/programming-praxis-convert-csv-to-html].
Sorry for the sloppy comment, I don’t know the markup required for the comments here.
[…] Pages: 1 2 […]
Ruby solution
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:
And here’s a longer (more awesome!) version that correctly deals with
quotes and translates what it can to Scheme values (numbers, symbols,
#t/#f, etc): blog post
[…] post is a Python solution to a programming challenge from Programming Praxis. The challenge is to read a csv file and convert it into an html […]
Here’s a solution in PHP:
<?php
define("DIRECTORY", "./");
if (file_exists(DIRECTORY."x.txt")) {
$xfile = fopen(DIRECTORY."x.txt", "r");
$xcontent = fgetcsv($xfile);
echo "
“;
foreach ($xcontent as $xentry) {
echo “”.$xentry.””;
}
echo ”
“;
} else die(“The file does not exist.”);
?>
Quotation marks delineated with a q:
<?php
define("DIRECTORY", "./");
if (file_exists(DIRECTORY."x.txt")) {
$xfile = fopen(DIRECTORY."x.txt", "r");
$xcontent = fgetcsv($xfile);
echo q
q;
foreach ($xcontent as $xentry) {
echo qq.$xentry.qq;
}
echo q
q;
} else die(“The file does not exist.”);
?>
Third time is NOT a charm… :/
CSV To HTML
Using JSP
CSV To HTML
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’)
for item in line:
col = et.SubElement(row, ‘td’)
col.text = item
return et.tostring(table, method=’html’).decode()
Not sure what happened to the formatting above.
C#:
C#:
erdalkiran, few comments on your solution:
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.
Thus, I would suggest to implement your approach something like this: