CSV to HTML translation: Difference between revisions

m
(→‎{{header|Java}}: Add version with streaming/StringJoiner)
 
(31 intermediate revisions by 16 users not shown)
Line 29:
=={{header|11l}}==
{{trans|C}}
<langsyntaxhighlight lang=11l>V input_csv = ‘Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
Line 48:
}, end' ‘’)
 
print("</td></tr>\n</table>")</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 57:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Ada}}==
Line 63:
{{libheader|AWS}}
csv2html.adb:
<langsyntaxhighlight lang=Ada>with Ada.Strings.Fixed;
with Ada.Text_IO;
with Templates_Parser;
Line 102:
(Templates_Parser.Parse ("table.tmplt", Translations));
end;
end Csv2Html;</langsyntaxhighlight>
 
table.tmplt:
<langsyntaxhighlight lang=html5><table>
@@TABLE@@
<tr>
Line 112:
</tr>
@@END_TABLE@@
</table></langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 140:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 146:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<langsyntaxhighlight lang=algol68>#!/usr/local/bin/a68g --script #
 
[6]STRING rows := []STRING(
Line 214:
elbat;
ydob;
lmth</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><HTML>
<HEAD>
<TITLE>CSV to HTML translation - Extra Credit</TITLE>
Line 234:
</TABLE>
</BODY>
</HTML></langsyntaxhighlight>
 
=={{header|ANTLR}}==
===Java===
<langsyntaxhighlight lang=java>
// Create an HTML Table from comma seperated values
// Nigel Galloway - June 2nd., 2013
Line 248:
field : Field{System.out.println("<TD>" + $Field.text.replace("<","&lt;").replace(">","&gt;") + "</TD>");};
Field : ~[,\n\r]+;
</syntaxhighlight>
</lang>
 
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang=gwbasic> 100 DATA "Character,Speech"
110 DATA "The multitude,The messiah! Show us the messiah!"
120 DATA "Brian's mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>"
130 DATA "The multitude,Who are you?"
140 DATA "Brian's mother,I'm his mother; that's who!"
150 DATA "The multitude,Behold his mother! Behold his mother!"
160 DATA
170 LET M$ = CHR$ (13)
180 LET Q$ = CHR$ (34)
190 LET TRUE = NOT FALSE
200 LET HEADER = TRUE
210 DIM C(255),H$(4,1)
220 LET H$(1,0) = "</TD><TD>"
230 LET H$(2,0) = "&LT;"
240 LET H$(3,0) = "&GT;"
250 LET H$(4,0) = "&AMP;"
260 FOR I = 1 TO 4
270 LET C( ASC ( MID$ (",<>&",I,1))) = I
280 LET H$(I,HEADER) = H$(I,0)
290 NEXT I
300 LET H$(1,1) = "</TH><TH>"
310 PRINT "<!DOCTYPE HTML>"M$"<HTML>"M$"<HEAD>"M$"</HEAD>"M$"<BODY>"
320 PRINT "<TABLE BORDER="Q$"1"Q$" CELLPADDING="Q$"10"Q$" CELLSPACING="Q$"0"Q$">"
330 READ CSV$
340 FOR Q = 0 TO 1 STEP 0
350 PRINT "<TR><T" MID$ ("DH",1 + HEADER,1)">";
360 FOR I = 1 TO LEN (CSV$)
370 LET C$ = MID$ (CSV$,I,1)
380 LET H = C( ASC (C$))
390 PRINT H$(H,HEADER) MID$ (C$,1,H = 0);
400 NEXT I
410 PRINT "</T" MID$ ("DH",1 + HEADER,1)"></TR>"
420 LET HEADER = FALSE
430 READ CSV$
440 LET Q = CSV$ = ""
450 NEXT Q
460 PRINT "</TABLE>"M$"</BODY>"M$"</HTML>"</syntaxhighlight>
=={{header|Arturo}}==
 
<syntaxhighlight lang=rebol>in: {
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
}
 
table: function [content]
-> join @["<table>" join content "</table>"]
 
row: function [data]
-> join @[
"<tr><td>" escape.xml first data "</td>"
"<td>" escape.xml last data "</td></tr>"
]
 
print table map read.csv in => row</syntaxhighlight>
 
{{out}}
 
<table><tr><td>Character</td><td>Speech</td></tr><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr><tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He&apos;s not the messiah; he&apos;s a very naughty boy! Now go away!&lt;/angry&gt;</td></tr><tr><td>The multitude</td><td>Who are you?</td></tr><tr><td>Brians mother</td><td>I&apos;m his mother; that&apos;s who!</td></tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></table>
 
=={{header|AutoHotkey}}==
Very basic implementation
<langsyntaxhighlight lang=AutoHotkey>CSVData =
(
Character,Speech
Line 276 ⟶ 340:
return str
}
MsgBox % clipboard := TableData</langsyntaxhighlight>
{{out}}
<pre><table>
Line 288 ⟶ 352:
 
=={{header|AutoIt}}==
<langsyntaxhighlight lang=AutoIt>
Local $ascarray[4] = [34,38,60,62]
$String = "Character,Speech" & @CRLF
Line 317 ⟶ 381:
$newstring &= "</table>"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $newstring = ' & $newstring & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
</syntaxhighlight>
</lang>
 
{{out}}
Line 354 ⟶ 418:
Includes extra credit.<br />
File csv2html.awk
<langsyntaxhighlight lang=awk>#!/usr/bin/awk -f
BEGIN {
FS=","
Line 377 ⟶ 441:
print "</table>"
}
</syntaxhighlight>
</lang>
 
<pre>$ awk -f csv2html.awk input.csv</pre>
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 405 ⟶ 469:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
Extra credit:
 
<pre>$ awk -v header=1 -f csv2html.awk input.csv</pre>
<langsyntaxhighlight lang=html5><table>
<tr>
<th>Character</th>
Line 435 ⟶ 499:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang=dos>::Batch Files are terrifying when it comes to string processing.
::But well, a decent implementation!
@echo off
Line 469 ⟶ 533:
endlocal
)
echo ^</table^></langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 478 ⟶ 542:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang=bbcbasic> DATA "Character,Speech"
DATA "The multitude,The messiah! Show us the messiah!"
DATA "Brian's mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>"
Line 524 ⟶ 588:
SYS "ShellExecute", @hwnd%, 0, "CSVtoHTML.htm", 0, 0, 1
</syntaxhighlight>
</lang>
{{out}}
<pre><HTML>
Line 549 ⟶ 613:
Note that right angle brackets are deliberately not escaped, since that is not strictly necessary for the markup to be valid.
 
<langsyntaxhighlight lang=Befunge><v_>#!,#:< "<table>" \0 +55
v >0>::65*1+`\"~"`!*#v_4-5v >
v>#^~^<v"<tr><td>" < \v-1/<>">elb"
Line 557 ⟶ 621:
>^>\#v_$$0";pma&" v>"/<>d"v5 v , <
$ > \#v_$0";tl&"v v"</t"<0 > : |
^_>#!,#:<>#<0#<\#<<< >:#,_$#^_v@ $<</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 566 ⟶ 630:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Bracmat}}==
===Extra credit solution using pattern matching ===
This is not the most concise solution, but it is relatively efficient. To collect the lines we use a pattern that matches a line starting from position <code>[!p</code>. Each time a line is matched, <code>p</code> is updated, the two found elements are collected and the pattern is forced to fail, so the pattern matcher finds the next line. The found rows are collected in reverse order, because prepending to a list is faster than appending. When all lines are read, the collected lines are reversed, interspersed with newline characters. Finally the predefined function toML is used to create HTML.
<langsyntaxhighlight lang=bracmat>( ( CSVtoHTML
= p q Character Speech swor rows row
. 0:?p
Line 602 ⟶ 666:
"
)
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight lang=html><table><thead><tr><th>Character</th><th>Speech</th></tr></thead>
<tbody><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td></tr>
Line 610 ⟶ 674:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</tbody></table></langsyntaxhighlight>
 
===Simple solution NOT using pattern matching ===
Line 619 ⟶ 683:
 
In the code below, we use <code>vap</code> with a third argument, a splitting separator character. The outer call to <code>vap</code> splits a text into rows. An embedded call to <code>vap</code> splits each row into cell elements. This code is very efficient.
<langsyntaxhighlight lang=Bracmat>( ( Csv2Html
=
. toML
Line 641 ⟶ 705:
The multitude,Behold his mother! Behold his mother!
"
)</langsyntaxhighlight>
 
===Extra credit solution===
<langsyntaxhighlight lang=Bracmat>( ( Csv2Html
=
. toML
Line 682 ⟶ 746:
The multitude,Behold his mother! Behold his mother!
"
)</langsyntaxhighlight>
 
Output:
<langsyntaxhighlight lang=html>
<table><thead><tr><th>Character</th><th>Speech</th></tr>
</thead><tbody><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 693 ⟶ 757:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
<tr><td /></tr>
</tbody></table></langsyntaxhighlight>
 
=={{header|C}}==
 
<langsyntaxhighlight lang=c>#include <stdio.h>
 
const char *input =
Line 725 ⟶ 789:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 732 ⟶ 796:
$ ./csv</pre>
 
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 739 ⟶ 803:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
 
===Simple Solution===
 
<langsyntaxhighlight lang=C sharp>
using System;
using System.Collections.Generic;
Line 778 ⟶ 842:
}
}
</syntaxhighlight>
</lang>
 
{{out}} when using the text suggested:
<langsyntaxhighlight lang=html5>
<table><tr><td>Character</td><td>Speech</td></tr><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr><tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He&#39;s not the messiah; he&#39;s a very naughty boy! Now go away!&lt;/angry&gt;</td></tr><tr><td>The multitude</td><td>Who are you?</td></tr><tr><td>Brians mother</td><td>I&#39;m his mother; that&#39;s who!</td></tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></table>
</syntaxhighlight>
</lang>
 
===Extra Credit Solution===
 
<langsyntaxhighlight lang=C sharp>using System;
using System.Linq;
using System.Net;
Line 840 ⟶ 904:
}
}
}</langsyntaxhighlight>
 
{{out|Sample HTML Output}}
<langsyntaxhighlight lang=html5><table>
<tr>
<th>Character</th>
Line 868 ⟶ 932:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang=cpp>#include <string>
#include <boost/regex.hpp>
#include <iostream>
Line 902 ⟶ 966:
tabletext.append( "</TABLE>\n" ) ;
return tabletext ;
}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5>
<TABLE>
<TR><TD>Character</TD><TD>Speech</TD></TR>
Line 913 ⟶ 977:
<TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR>
</TABLE>
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
Line 919 ⟶ 983:
We assume the presence of a file, but the input could come from anywhere.
 
<langsyntaxhighlight lang=csv>
Character,Speech
The multitude,The messiah! Show us the messiah!
Line 926 ⟶ 990:
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang=clojure>
(require 'clojure.string)
 
Line 960 ⟶ 1,024:
tbl (map #(clojure.string/split % #",") lines)]
(println (to-html tbl)))
</syntaxhighlight>
</lang>
 
{{out}}
<langsyntaxhighlight lang=html>
<table><tbody><tr><td>Character</td><td>Speech</td></tr><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr><tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td></tr><tr><td>The multitude</td><td>Who are you?</td></tr><tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></tbody></thead>
</syntaxhighlight>
</lang>
 
=={{header|CoffeeScript}}==
Line 971 ⟶ 1,035:
{{works with|node.js}}
 
<langsyntaxhighlight lang=coffeescript>String::__defineGetter__ 'escaped', () ->
this.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
Line 1,009 ⟶ 1,073:
</tbody>
</table>
"""</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table cellspacing="0">
<thead>
<th scope="col">Character</th>
Line 1,029 ⟶ 1,093:
<td>Behold his mother! Behold his mother!</td>
</tbody>
</table></langsyntaxhighlight>
 
=={{header|Common Lisp}}==
 
<langsyntaxhighlight lang=lisp>(defvar *csv* "Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
Line 1,085 ⟶ 1,149:
(dolist (row rows)
(format html "~C~A~C" #\Tab (html-row row nil) #\Newline))
(write-string "</table>" html))))</langsyntaxhighlight>
 
<pre>CL-USER> (csv->html *csv*)</pre>
 
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr><th>Character</th><th>Speech</th></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 1,097 ⟶ 1,161:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang=d>void main() {
import std.stdio;
 
Line 1,137 ⟶ 1,201:
 
"</td></tr>\n</tbody>\n</table>\n\n</body></html>".write;
}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><html>
<head><meta charset="utf-8"></head>
<body>
Line 1,157 ⟶ 1,221:
 
</body>
</html></langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 1,163 ⟶ 1,227:
This solution solves both the basic and extra credit tasks.
 
<langsyntaxhighlight lang=Delphi>program csv2html;
 
{$APPTYPE CONSOLE}
Line 1,335 ⟶ 1,399:
// Keep console window open
Readln;
end.</langsyntaxhighlight>
 
{{out|Basic output}}
<langsyntaxhighlight lang=html5><!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Line 1,366 ⟶ 1,430:
</table>
</body>
</html></langsyntaxhighlight>
 
{{out|Extra credit output}}
<langsyntaxhighlight lang=html5><!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
Line 1,397 ⟶ 1,461:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
h$ = "<table border=1>\n<tr><th>"
repeat
s$ = input
until s$ = ""
write h$
for c$ in strchars s$
if c$ = ","
if scnd = 0
c$ = "</th><th>"
else
c$ = "</td><td>"
.
elif c$ = "<"
c$ = "&lt;"
elif c$ = ">"
c$ = "&gt;"
elif c$ = "&"
c$ = "&amp;"
.
write c$
.
if scnd = 0
h$ = "</th></tr>\n<tr><td>"
scnd = 1
else
h$ = "</td></tr>\n<tr><td>"
.
.
print "</td></tr>\n</table>"
#
input_data
Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
</syntaxhighlight>
 
{{out}}
<table border=1>
<tr><th>Character</th><th>Speech</th></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang=scheme>
;; CSV -> LISTS
(define (csv->row line) (string-split line ","))
Line 1,431 ⟶ 1,546:
(style 'table "border-spacing: 10px;border:28px ridge orange") ;; special biblical border
(style 'th "color:blue;")
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang=scheme>
;; changed <angry> to <b> to show that html tags inside text are correctly transmitted.
(define MontyPython #<<
Line 1,451 ⟶ 1,566:
(task MontyPython)
</syntaxhighlight>
</lang>
 
<table style='border-spacing: 10px;border:28px ridge orange'> <tr> <th style='color:blue;'> Character </th> <th style='color:blue;'> Speech </th> </tr> <tr> <td style='text-align:left'> The multitude </td> <td style='text-align:left'> The messiah! Show us the messiah! </td> </tr> <tr> <td style='text-align:left'> Brians mother </td> <td style='text-align:left'> <b>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</b> </td> </tr> <tr> <td style='text-align:left'> The multitude </td> <td style='text-align:left'> Who are you? </td> </tr> <tr> <td style='text-align:left'> Brians mother </td> <td style='text-align:left'> I'm his mother; that's who! </td> </tr> <tr> <td style='text-align:left'> The multitude </td> <td style='text-align:left'> Behold his mother! Behold his mother! </td> </tr> </table>
Line 1,457 ⟶ 1,572:
=={{header|Erlang}}==
Using functions from [[Create_an_HTML_table]]
<langsyntaxhighlight lang=Erlang>
-module( csv_to_html ).
 
Line 1,478 ⟶ 1,593:
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!".
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,486 ⟶ 1,601:
{{trans|C}}
{{works with|Euphoria|4.*}}
<langsyntaxhighlight lang=euphoria>constant input = "Character,Speech\n" &
"The multitude,The messiah! Show us the messiah!\n" &
"Brians mother,<angry>Now you listen here! He's not the messiah; " &
Line 1,505 ⟶ 1,620:
end switch
end for
puts(1,"</td></tr>\n</table>")</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 1,515 ⟶ 1,630:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Use .NET XmlWriter.
Stylesheet styling is applied only when command line option <tt>-h</tt> ist given.
<langsyntaxhighlight lang=fsharp>open System
open System.Text
open System.Xml
Line 1,573 ⟶ 1,688:
x.WriteEndElement() // tr
x.Close()
0</langsyntaxhighlight>
{{out}} (stylesheet version)
<div style="font-size:70%">
<langsyntaxhighlight lang=html5><?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE HTML >
<html>
Line 1,620 ⟶ 1,735:
</table>
</body>
</html></langsyntaxhighlight></div>
 
=={{header|Factor}}==
<syntaxhighlight lang =factor>USING: combinators csv io kernelhtml.streams sequencesprettyprint stringsxml.writer ;
IN: rosetta-code.csv-to-html
 
CONSTANT: input
 
"Character,Speech
Line 1,635 ⟶ 1,747:
The multitude,Behold his mother! Behold his mother!"
 
string>csv [ simple-table. ] with-html-writer pprint-xml</syntaxhighlight>
: escape-chars ( seq -- seq' )
[
{
{ CHAR: & [ "&amp;" ] }
{ CHAR: ' [ "&apos;" ] }
{ CHAR: < [ "&lt;" ] }
{ CHAR: > [ "&gt;" ] }
[ 1string ]
} case
] { } map-as concat ;
: tag ( str tag -- <tag>str</tag> )
[ "<" ">" surround ] [ "</" ">" surround ] bi surround ;
: csv>table ( seq -- str )
[ [ "td" tag ] map concat "tr" tag " " prepend ] map
{ "<table>" } prepend { "</table>" } append "\n" join ;
input escape-chars string>csv csv>table print</lang>
{{out}}
<syntaxhighlight lang=html5><table style="display: inline-table; border-collapse: collapse;">
<lang html5><table>
<tr>
<tr><td>Character</td><td>Speech</td></tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Character
<tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He&apos;s not the messiah; he&apos;s a very naughty boy! Now go away!&lt;/angry&gt;</td></tr>
</td>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
<tr><td>Brians mother</td><td>I&apos;m his mother; that&apos;s who!</td></tr>
Speech
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</td>
</table></lang>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
The multitude
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
The messiah! Show us the messiah!
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Brians mother
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
&lt;angry&gt;Now you listen here! He's not the messiah; he's a very
naughty boy! Now go away!&lt;/angry&gt;
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
The multitude
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Who are you?
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Brians mother
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
I'm his mother; that's who!
</td>
</tr>
<tr>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
The multitude
</td>
<td valign="top" style="border: 1px solid #cccccc; padding: 2px; ">
Behold his mother! Behold his mother!
</td>
</tr>
</table>
<br/></syntaxhighlight>
 
=={{header|Forth}}==
Line 1,669 ⟶ 1,807:
{{trans|C}}
 
<langsyntaxhighlight lang=forth>: BEGIN-COLUMN ." <td>" ;
: END-COLUMN ." </td>" ;
 
Line 1,690 ⟶ 1,828:
;
 
CSV2HTML BYE</langsyntaxhighlight>
 
{{out}}
 
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 1,701 ⟶ 1,839:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Fortran}}==
Line 1,713 ⟶ 1,851:
 
The check for a comma is not <code>ALINE(I:I).EQ.","</code> because in other work this usage has been found to evoke astoundingly bad code, notably that ''both'' appearances of "I" are checked as being within bounds, and, the length is calculated by subtracting the "first" (I) from the "last" (also I) at run time! At least by the COMPAQ F90/95 compiler. By contrast, the ICHAR usage, which can only be for a single character, lacks this madness and far superior speed results. Not important in this example, but it explains why this puzzling usage appeared in a prog. at the Culham Science Centre in source from an IBM mainframe.
<langsyntaxhighlight lang=Fortran>
SUBROUTINE CSVTEXT2HTML(FNAME,HEADED) !Does not recognise quoted strings.
Converts without checking field counts, or noting special characters.
Line 1,785 ⟶ 1,923:
CALL CSVTEXT2HTML("Text.csv",.TRUE.) !The first line is a heading.
END
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 1,828 ⟶ 1,966:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</Table>
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang=freebasic>Data "Character,Speech"
Data "The multitude,The messiah! Show us the messiah!"
Data "Brian's mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>"
Data "The multitude,Who are you?"
Data "Brian's mother,I'm his mother; that's who!"
Data "The multitude,Behold his mother! Behold his mother!"
Data "***"
 
Print "<!DOCTYPE html>" & Chr(10) & "<html>"
Print "<head>"
Print "</head>" & Chr(10)
Print "<body>"
Print "<h1 style=""text-align:center"">CSV to html translation </h1>"
Print: Print "<table border = 1 cellpadding = 10 cellspacing = 0>"
 
Dim As Boolean header = true
Dim As String cadenaCSV, txt
Do
Read cadenaCSV
If cadenaCSV = "***" then Exit Do
If header then
Print "<thead bgcolor=""green"">" & Chr(10) & "<tr><th>";
Else
Print "<tr><td>";
End If
For i As Integer = 1 To Len(cadenaCSV)
txt = Mid(cadenaCSV, i, 1)
Select Case txt
Case ",": If header then Print "</th><th>"; Else Print "</td><td>";
Case "<": Print "&lt;";
Case ">": Print "&gt;";
Case "&": Print "&amp;";
Case Else: Print txt;
End Select
Next i
If header then
Print "</th></tr>" & Chr(10) & "</thead>" & Chr(10) & "<tbody bgcolor=""yellow"">"
Else
Print "</td></tr>"
End If
header = false
Loop Until false
 
Print "</tbody>" & Chr(10) & "</table>"
Print Chr(10) & "</body>"
Print "</html>"
Sleep</syntaxhighlight>
 
 
 
 
=={{header|FutureBasic}}==
FB has a native Macintosh web browser, so the HTML output, along with extra credit, is shown in the accompanying screenshot. This code produces a complete, launchable application.
<syntaxhighlight lang=futurebasic>
output file "CSV2HTML"
 
include "Tlbx WebKit.incl"
 
_window = 1
begin enum output 1
_webView
end enum
 
void local fn BuildWindow
CGRect r = fn CGRectMake( 0, 0, 600, 220 )
window _window, @"Rosetta Code CSV to HTML", r, NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
r = fn CGRectMake( 20, 20, 560, 180 )
wkwebview _webView, r,, _window
end fn
 
local fn CSV2HTML as CFStringRef
NSUInteger i, count
CFStringRef csvStr = @"Character,Speech\n¬
The multitude,The messiah! Show us the messiah!\n¬
Brians mother,Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!\n¬
The multitude,Who are you\n¬
Brians mother,I'm his mother; that's who!\n¬
The multitude,Behold his mother! Behold his mother!"
CFArrayRef linesArray = fn StringComponentsSeparatedByString( csvStr, @"\n" )
CFMutableStringRef htmlStr = fn MutableStringWithCapacity(0)
MutableStringAppendString( htmlStr, @"<table style=\"background:#eee;\">\n" )
MutableStringAppendString( htmlStr, @"<tr bgcolor=wheat><th>Character</th><th>Speech</th></tr>" )
MutableStringAppendString( htmlStr, @"<caption>From Monty Python's \"The Life of Brian\"</caption>\n" )
count = len( linesArray )
for i = 1 to count - 1
CFStringRef tempStr = linesArray[i]
CFArrayRef tempArr = fn StringComponentsSeparatedByString( tempStr, @"," )
MutableStringAppendString( htmlStr, @"<tr>\n" )
MutableStringAppendString( htmlStr, fn StringWithFormat( @"<td style=\"width:120px;\"><b>%@</b></td>>\n", tempArr[0] ) )
MutableStringAppendString( htmlStr, fn StringWithFormat( @"<td><i>%@</i></td>\n", tempArr[1] ) )
MutableStringAppendString( htmlStr, @"</tr>\n" )
next
MutableStringAppendString( htmlStr, @"</table><br></br>" )
end fn = fn StringWithString( htmlStr )
 
local fn LoadHTML2WebView
CFStringRef htmlStr = fn CSV2HTML
fn WKWebViewLoadHTMLString( _webView, htmlStr, NULL )
end fn
 
void local fn DoDialog( ev as long, tag as long, wnd as long, obj as CFTypeRef )
select (ev)
case _windowWillClose : end
end select
end fn
 
on dialog fn DoDialog
 
fn BuildWindow
fn LoadHTML2WebView
 
HandleEvents
</syntaxhighlight>
{{output}}
[[File:CSV to HTML.png]]
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,866 ⟶ 2,127:
`)).Execute(&b, data)
return b.String(), err
}</langsyntaxhighlight>
Extra credit version accepts -h command line option to do the special formatting for the heading line.
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,924 ⟶ 2,185:
</table>
`
)</langsyntaxhighlight>
{{out}}
Extra credit version with -h
<langsyntaxhighlight lang=html><table>
<thead>
<tr><th>Character</th><th>Speech</th></tr>
Line 1,938 ⟶ 2,199:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</tbody>
</table></langsyntaxhighlight>
Basic version, or extra credit version without -h
<langsyntaxhighlight lang=html><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 1,947 ⟶ 2,208:
<tr><td>Brians mother</td><td>I&#39;m his mother; that&#39;s who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Groovy}}==
Line 1,953 ⟶ 2,214:
===Solution #1: Nested GStrings===
Brute force solution using nested GStrings. It solves both the basic and extra credit tasks.
<langsyntaxhighlight lang=groovy>def formatCell = { cell ->
"<td>${cell.replaceAll('&','&amp;').replaceAll('<','&lt;')}</td>"
}
Line 1,990 ⟶ 2,251:
<body>${formatTable(csv, header)}</body>
</html>"""
}</langsyntaxhighlight>
 
'''Test:'''
<langsyntaxhighlight lang=groovy>def csv = '''Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
Line 2,009 ⟶ 2,270:
println '-----------------------------------------'
println (formatPage('Extra Credit', csv, true))
println '-----------------------------------------'</langsyntaxhighlight>
 
{{out|Basic output}}
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><html>
<head>
<title>Basic</title>
Line 2,030 ⟶ 2,291:
</table>
</body>
</html></langsyntaxhighlight></div>
[[File:Groovy-csv-to-html-basic.jpg]] <br>Appearance as rendered in Google Chrome.
 
{{out|Extra Credit output}}
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><html>
<head>
<title>Extra Credit</title>
Line 2,056 ⟶ 2,317:
</table>
</body>
</html></langsyntaxhighlight></div>
[[File:Groovy-csv-to-html-extra.jpg]] <br>Appearance as rendered in Google Chrome.
 
===Solution #2: MarkupBuilder===
A much cleaner solution using the Groovy XML MarkupBuilder class. It solves both the basic and extra credit tasks.
<langsyntaxhighlight lang=groovy>import groovy.xml.MarkupBuilder
 
def formatRow = { doc, row ->
Line 2,090 ⟶ 2,351:
}
writer.toString()
}</langsyntaxhighlight>
 
'''Test:'''<br/>
Line 2,096 ⟶ 2,357:
 
{{out|Basic output}}
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><html>
<head>
<title>Basic</title>
Line 2,132 ⟶ 2,393:
</table>
</body>
</html></langsyntaxhighlight></div>
 
The HTML for this solution looks superficially different than that from the GString solution, but the appearance as rendered in Google Chrome is identical.
 
'''Extra Credit output:'''
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><html>
<head>
<title>Extra Credit</title>
Line 2,177 ⟶ 2,438:
</table>
</body>
</html></langsyntaxhighlight></div>
 
The HTML for this solution looks superficially different than that from the GString solution, but the appearance as rendered in Google Chrome is identical.
Line 2,183 ⟶ 2,444:
=={{header|Haskell}}==
'''Simple solution'''
<langsyntaxhighlight lang=haskell>--import Data.List.Split (splitOn) -- if the import is available
splitOn :: Char -> String -> [String] -- otherwise
splitOn delim = foldr (\x rest ->
Line 2,208 ⟶ 2,469:
in "<table>\n" ++ html ++ "</table>"
 
main = interact csvToTable</langsyntaxhighlight>
 
'''Compact version'''
<langsyntaxhighlight lang=haskell>import Data.List (unfoldr)
split p = unfoldr (\s -> case dropWhile p s of [] -> Nothing
ss -> Just $ break p ss)
Line 2,221 ⟶ 2,482:
'&' -> "&amp;"; '"' -> "&quot;"; _ -> [c]}) x
++ "</td>\n") cols)
++ "</tr>") . split (==',')) $ lines csv) ++ "</table>")</langsyntaxhighlight>
 
;Output:
<div style="height:30ex;overflow:scroll;"><langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 2,249 ⟶ 2,510:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight></div>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 2,257 ⟶ 2,518:
to the procedure writes or a 1 (for more on this see [[Icon%2BUnicon/Intro#Conjunction.2C_yielding_a_different_result|Introduction to Icon/Unicon - Conjunction yielding different results]]).
 
<langsyntaxhighlight lang=Icon>procedure main(arglist)
pchar := &letters ++ &digits ++ '!?;. ' # printable chars
 
Line 2,276 ⟶ 2,537:
write(" </TBODY>")
write("</TABLE>")
end</langsyntaxhighlight>
 
{{Out}}
<langsyntaxhighlight lang=html5><TABLE>
<THEAD>
<TR><TD>Character</TD><TD>Speech</TD></TR>
Line 2,290 ⟶ 2,551:
<TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR>
</TBODY>
</TABLE></langsyntaxhighlight>
 
=={{header|J}}==
'''Solution (extra credit)'''
<langsyntaxhighlight lang=j>require 'strings tables/csv'
encodeHTML=: ('&';'&amp;';'<';'&lt;';'>';'&gt;')&stringreplace
 
Line 2,315 ⟶ 2,576:
end.
;markupTable markupRows t
)</langsyntaxhighlight>
 
For those interested, equivalent tacit versions of <code>tag</code> and <code>makeHTMLtablefromCSV</code> are:
<langsyntaxhighlight lang=j>tag=: adverb def '[: (,&.>/)"1 m&(0&{::@[ , 1&{::@[ ,~ ]) L:0@]'
makeHTMLtablefromCSV6=: 0&$: : ([: ; markupTable@markupRows@([ markupCells`(markupHdrCells@{. , markupCells@}.)@.[ fixcsv@encodeHTML))</langsyntaxhighlight>
 
'''Example'''
<langsyntaxhighlight lang=j> CSVstrng=: noun define
Character,Speech
The multitude,The messiah! Show us the messiah!
Line 2,330 ⟶ 2,591:
The multitude,Behold his mother! Behold his mother!
)
1 makeHTMLtablefromCSV CSVstrng</langsyntaxhighlight>
{{Out|HTML output}}
<langsyntaxhighlight lang=html5><table>
<tr><th>Character</th><th>Speech</th></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 2,339 ⟶ 2,600:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Java}}==
'''Simple solution without escaping for Java v8+'''
 
<langsyntaxhighlight lang=java>String csv = "...";
// Use Collectors.joining(...) for streaming, otherwise StringJoiner
StringBuilder html = new StringBuilder("<table>\n");
Line 2,351 ⟶ 2,612:
html.append(Arrays.stream(row.split(",")).collect(collector));
}
html.append("</table>\n");</langsyntaxhighlight>
 
'''Solution including simple and extra credit version'''
Line 2,359 ⟶ 2,620:
<tt>for extended solution: java -cp . Csv2Html header < text.csv</tt>
 
<langsyntaxhighlight lang=java>import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
Line 2,445 ⟶ 2,706:
}
}
</syntaxhighlight>
</lang>
 
{{out|simple solution}}
<langsyntaxhighlight lang=html5><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
Line 2,465 ⟶ 2,726:
<tr><td>Brians mother</td><td>I&apos;m his mother; that&apos;s who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></body></html></langsyntaxhighlight>
 
{{out|extended}}
<langsyntaxhighlight lang=html5><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
Line 2,485 ⟶ 2,746:
<tr><td>Brians mother</td><td>I&apos;m his mother; that&apos;s who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></body></html></langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang=JavaScript>var csv = "Character,Speech\n" +
"The multitude,The messiah! Show us the messiah!\n" +
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n" +
Line 2,507 ⟶ 2,768:
'\t</tbody>\n</table>');
 
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang=html5><table>
<thead>
<tr><td>Character</td><td>Speech</td></tr>
Line 2,520 ⟶ 2,781:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</tbody>
</table></langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table>
<thead>
<tr><td>Character</td><td>Speech</td></tr>
Line 2,533 ⟶ 2,794:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr> </tbody>
</table></langsyntaxhighlight>
 
=={{header|jq}}==
We will assume the input is in a file named csv2html.csv, and that the jq program as given below is in a file named csv2html.jqn. To simplify things, we will invoke the jq processor twice -- the first invocation simply converts the text input into a sequence of JSON strings:
<langsyntaxhighlight lang=jq>jq -R . csv2html.csv | jq -r -s -f csv2html.jq
</langsyntaxhighlight><syntaxhighlight lang =jq>def headerrow2html:
[" <thead> <tr>"]
+ (split(",") | map(" <th>\(@html)</th>"))
Line 2,559 ⟶ 2,820:
;
 
csv2html | .[]</langsyntaxhighlight>
 
===Output===
<langsyntaxhighlight lang=html5><table>
<thead> <tr>
<th>Character</th>
Line 2,587 ⟶ 2,848:
<td>Behold his mother! Behold his mother! </td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|Jsish}}==
From Javascript entry.
 
<langsyntaxhighlight lang=javascript>/* CSV to HTML, in Jsish */
var csv = "Character,Speech\n" +
"The multitude,The messiah! Show us the messiah!\n" +
Line 2,627 ⟶ 2,888:
</table>
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 2,634 ⟶ 2,895:
 
=={{header|Julia}}==
<langsyntaxhighlight lang=Julia>using DataFrames, CSV
 
using CSV, DataFrames
Line 2,704 ⟶ 2,965:
print(csv2html("input.csv", header=true))
</langsyntaxhighlight>{{out}}
<langsyntaxhighlight lang=html5><html>
 
<head>
Line 2,769 ⟶ 3,030:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang=scala>// version 1.1.3
 
val csv =
Line 2,825 ⟶ 3,086:
sb.append("</td>\n$i$i</tr>\n$i</tbody>\n</table>")
println(sb.toString())
}</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 2,885 ⟶ 3,146:
</tbody>
</table>
</syntaxhighlight>
</lang>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang=scheme>
{def CSV
Character,Speech\n
The multitude,The messiah! Show us the messiah!\n
Brians mother,Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!\n
The multitude,Who are you\n
Brians mother,I'm his mother; that's who!\n
The multitude,Behold his mother! Behold his mother!\n
}
-> CSV
 
{def csv2html
{lambda {:csv}
{table {@ style="background:#eee;"}
{S.replace ([^,]*),([^_]*)_
by {tr {td {@ style="width:120px;"}{b €1}} {td {i €2}}}
in {S.replace \\n by _ in :csv}}}}}
-> csv2html
 
{csv2html {CSV}} ->
</syntaxhighlight>
{{out}}
<table style="background:#eee;"><tr><td style="width:120px;"><b>Character</b></td> <td><i>Speech</i></td></tr><tr><td style="width:120px;"><b> The multitude</b></td> <td><i>The messiah! Show us the messiah!</i></td></tr><tr><td style="width:120px;"><b> Brians mother</b></td> <td><i>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</i></td></tr><tr><td style="width:120px;"><b> The multitude</b></td> <td><i>Who are you</i></td></tr><tr><td style="width:120px;"><b> Brians mother</b></td> <td><i>I'm his mother; that's who!</i></td></tr><tr><td style="width:120px;"><b> The multitude</b></td> <td><i>Behold his mother! Behold his mother!</i></td></tr></table>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang=lb>
<lang lb>
newline$ ="|"
' No escape behaviour, so can't refer to '/n'.
Line 2,925 ⟶ 3,211:
print "</HTML>"
end
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,953 ⟶ 3,239:
 
=={{header|Lua}}==
<langsyntaxhighlight lang=lua>FS = "," -- field separator
 
csv = [[
Line 2,980 ⟶ 3,266:
for _, line in pairs(html) do
print(line)
end</langsyntaxhighlight>
 
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 2,989 ⟶ 3,275:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
 
<syntaxhighlight lang="m2000 interpreter">
module csv2html {
nl$={
}
Repl$=lambda$ (a$) ->{
a$=replace$("&", "&amp;",a$)
a$=replace$(">", "&gt;",a$)
a$=replace$("""", "&quot;",a$)
// add any other replacement here
=replace$("<", "&lt;",a$)
}
Tag$=lambda$ nl$, repl$ (a$, b$, n=4)->{
if n>0 then
a$=rtrim$(replace$(nl$, nl$+string$(" ", n), nl$+a$))
if right$(a$,2)<>nl$ then a$+=nl$
else
if right$(a$, 2)=nl$ then if left$(a$,2)<>nl$ then a$=nl$+a$
end if
prop=(,) : Read ? prop // forth parameter optional (we have to initalize first with an empty array)
p=each(prop) : prop$="" // p is an iteration object.
while p
prop$+=" "+repl$(prop#val$(p^))+"="+quote$(repl$(prop#val$(p^+1)))
p=each(prop,p^+2) // start new from p^+2 (p^ is the internal counter)
end while
="<"+b$+prop$+">"+a$+"</"+b$+">"+nl$
}
// Prepare FILE csv
tofile$={Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!
}
open "forHtml.csv" for wide output as #f
Print #f, tofile$;
close #f
// prepare the input style for Input from file statement
// "," - we use comma between fields
// "." - for numbers we use dot for decimal separator
// false - we didn't read json style strings to normal strings (so \n convert to code 13, \t to code 9)
// true - we read strings unquote
input with ",",".",false, true
// Read csv file
i=1
document export$=""
Open "forHtml.csv" for wide input as #f
while not eof(#f)
input #f, a$, b$
a$=repl$(a$)
b$=repl$(b$)
if i=1 then
export$+=tag$(tag$(a$, "th", 0)+tag$(b$,"th", 0), "tr", 4)
else
export$+=tag$(tag$(a$, "td", 0)+tag$(b$,"td", 0), "tr", 4)
end if
i++
end while
close #f
style$=tag$({TD {background-color:#ddddff; }
thead TD {background-color:#ddffdd; text-align:center; }
},"style",4,("type", "text/css"))
title$= tag$("CSV to HTML translation - Extra Credit","title",0)
Head$= tag$(title$+ style$,"head")
html$={<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">}+nl$+tag$(head$+tag$(tag$(export$, "table"), "body"), "html")
clipboard html$
Report html$
Print "Press Esc to exit browser"
browser "about: "+html$
}
csv2html
</syntaxhighlight>
{{out}}
<pre>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>CSV to HTML translation - Extra Credit</title>
<style type="text/css">
TD {background-color:#ddddff; }
thead TD {background-color:#ddffdd; text-align:center; }
</style>
</head>
<body>
<table>
<tr>
<th>Character</th>
<th>Speech</th>
</tr>
<tr>
<td>The multitude</td>
<td>The messiah! Show us the messiah!</td>
</tr>
<tr>
<td>Brians mother</td>
<td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr>
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</table>
</body>
</html>
</pre>
 
=={{header|Maple}}==
<langsyntaxhighlight lang=maple> #A translation of the C code posted
html_table := proc(str)
local char;
Line 3,020 ⟶ 3,424:
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!");
</syntaxhighlight>
</lang>
{{Out|Output}}
<pre>
Line 3,034 ⟶ 3,438:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>a="Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah;he's a very naughty boy! Now go away!</angry>
Line 3,050 ⟶ 3,454:
StringSplit[StringReplace[a,{","->"</td><td>","<"->"&lt;",">"->"&gt;"}],"\n"]//Rest]
,"</table>"]
</syntaxhighlight>
</lang>
{{Out|Naive}}
<pre>
Line 3,081 ⟶ 3,485:
split it into a cell array, and print it out specifying format.
 
<langsyntaxhighlight lang=MATLAB>
inputString = fileread(csvFileName);
% using multiple regular expressions to clear up special chars
Line 3,097 ⟶ 3,501:
cellfun(@(x)fprintf(1,['\n\t<tr>' sprintf('\n\t\t<td>%s</td>',x{:}) '\n\t</tr>']),tableValues(2:end))
fprintf(1,'\n</table>')
</syntaxhighlight>
</lang>
As a single line:
<langsyntaxhighlight lang=MATLAB>
fprintf(1,['<table>\n\t<tr>\n\t\t<th>',regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(regexprep(fileread(cvsFileName),'&','&amp;'),'"','&quot;'),'<','&lt;'),'>','&gt;'),'(?<=(^[^\n\r]*)),','</th>\n\t\t<th>'),'(?<!>)(\r\n|\r|\n)','</td>\n\t<tr>\n\t</tr>\n\t\t<td>'),'</td>\n','</th>\n','once'),',','</td>\n\t\t<td>'),'</td>\n\t</tr>\n</table>\n'])
</syntaxhighlight>
</lang>
 
{{Out}}
<langsyntaxhighlight lang=html5>
<table>
<tr>
Line 3,130 ⟶ 3,534:
</tr>
</table>
</syntaxhighlight>
</lang>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang=Maxima>infile: "input.csv";
outfile: "table.html";
instream: openr(infile);
Line 3,153 ⟶ 3,557:
 
close(instream);
close(outstream);</langsyntaxhighlight>
 
<langsyntaxhighlight lang=html5><TABLE border="1">
<THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY>
Line 3,162 ⟶ 3,566:
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY>
</TABLE></langsyntaxhighlight>
 
=={{header|ML/I}}==
This example carries out the special formatting for extra credit. This is a macro rather than a function, though, due to the nature of ML/I.
===Input===
<langsyntaxhighlight lang=ML/I>MCSKIP "WITH" NL
"" CSV to HTML
"" assumes macros on input stream 1, terminal on stream 2
Line 3,220 ⟶ 3,624:
</body>
</html>
]</langsyntaxhighlight>
===Output===
<langsyntaxhighlight lang=ML/I><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
Line 3,272 ⟶ 3,676:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight lang=nanoquery>// a method that converts a csv row into a html table row as a string
def toHtmlRow(record, tag)
htmlrow = "\t<tr>\n"
Line 3,306 ⟶ 3,710:
htmltable = htmltable+"</table>"
println htmltable</langsyntaxhighlight>
 
=={{header|NetRexx}}==
Uses the [[NetRexx]] solution for [[Read_a_file_line_by_line#Using_Java_Scanner|Read a file line by line]] to read the CSV file into the program.
<langsyntaxhighlight lang=NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 3,430 ⟶ 3,834:
|| ''
return html
</syntaxhighlight>
</lang>
'''Output:'''
<div style="height:30ex;overflow:scroll;">
<langsyntaxhighlight lang=html5><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
Line 3,516 ⟶ 3,920:
</body>
</html>
</syntaxhighlight>
</lang>
</div>
 
Line 3,525 ⟶ 3,929:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang=nim>import cgi, strutils
 
const csvtext = """Character,Speech
Line 3,534 ⟶ 3,938:
The multitude,Behold his mother! Behold his mother!"""
 
proc row2tr(row: string): string =
result = "<tr>"
let cols = xmlEncode(row).split(",")
Line 3,541 ⟶ 3,945:
result.add "</tr>"
 
proc csv2html(txt: string): string =
result = "<table summary=\"csv2html program output\">\n"
for row in txt.splitLines():
Line 3,547 ⟶ 3,951:
result.add "</table>"
 
echo csv2html(csvtext)</langsyntaxhighlight>
{{Out}}
<pre><table summary="csv2html program output">
Line 3,560 ⟶ 3,964:
=={{header|Oberon-2}}==
{{Works with|oo2c Version 2}}
<langsyntaxhighlight lang=oberon2>
MODULE CSV2HTML;
IMPORT
Line 3,633 ⟶ 4,037:
fileChannel.Close()
END CSV2HTML.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,648 ⟶ 4,052:
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight lang=objeck>use System.IO.File;
use Data.CSV;
 
Line 3,719 ⟶ 4,123:
}
}
</syntaxhighlight>
</lang>
 
Output:
Line 3,732 ⟶ 4,136:
but we do not use it hereafter because the CSV data does not contain comas.
 
<langsyntaxhighlight lang=ocaml>open Printf
 
let csv_data = "\
Line 3,766 ⟶ 4,170:
 
let () =
print_html_table (list_of_csv csv_data)</langsyntaxhighlight>
 
{{Out|Sample html output}}
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 3,776 ⟶ 4,180:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table> </langsyntaxhighlight>
 
===Extra credit solution===
 
<langsyntaxhighlight lang=ocaml>open Printf
 
let csv_data = "\
Line 3,823 ⟶ 4,227:
 
let () =
print_html_table (list_of_csv csv_data)</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><html>
<head>
<style type="text/css">
Line 3,842 ⟶ 4,246:
</tbody>
</table>
</html></langsyntaxhighlight>
 
=={{header|OpenEdge/Progress}}==
<langsyntaxhighlight lang=Progress (OpenEdge ABL)>
FUNCTION csvToHtml RETURNS CHARACTER (
i_lhas_header AS LOGICAL,
Line 3,895 ⟶ 4,299:
"The multitude,Behold his mother! Behold his mother!"
)
VIEW-AS ALERT-BOX.</langsyntaxhighlight>
{{out|with header enabled}}
<langsyntaxhighlight lang=html><html>
<table>
<tr>
Line 3,924 ⟶ 4,328:
</tr>
</table>
</html></langsyntaxhighlight>
 
=={{header|Perl}}==
Line 3,930 ⟶ 4,334:
Provide the CSV data as standard input. With a command-line argument, the first row will use <code><th></code> instead of <code><td></code>.
 
<langsyntaxhighlight lang=perl>use HTML::Entities;
 
sub row {
Line 3,944 ⟶ 4,348:
row @ARGV ? 'th' : 'td', $first;
row 'td', $_ foreach @rest;
print "</table>\n";</langsyntaxhighlight>
 
{{Out|Output (with a command-line argument)}}
<langsyntaxhighlight lang=html5><table>
<tr><th>Character</th><th>Speech</th></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 3,954 ⟶ 4,358:
<tr><td>Brians mother</td><td>I&#39;m his mother; that&#39;s who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Phix}}==
CopyBased ofon [[CSV_to_HTML_translation#Euphoria|Euphoria]] but with a simpler multiline constant.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant input = "Character,Speech\n" &
<span style="color: #008080;">with javascript_semantics</span>
"The multitude,The messiah! Show us the messiah!\n" &
<span style="color: #008080;">constant</span> <span style="color: #000000;">input</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
"Brians mother,<angry>Now you listen here! He's not the messiah; " &
Character,Speech
"he's a very naughty boy! Now go away!</angry>\n" &
" The multitude,WhoThe messiah! Show areus you?\n"the &messiah!
Brians mother,&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;
"Brians mother,I'm his mother; that's who!\n" &
" The multitude,Behold his mother! BeholdWho hisare mother!"you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!"""</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&lt;table&gt;\n&lt;tr&gt;&lt;td&gt;"</span><span style="color: #0000FF;">)</span>
puts(1,"<table>\n<tr><td>")
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">input</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
for i = 1 to length(input) do
<span style="color: #008080;">switch</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">do</span>
switch input[i] do
<span style="color: #008080;">case</span> <span style="color: #008000;">'\n'</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&lt;/td&gt;&lt;/tr&gt;\n&lt;tr&gt;&lt;td&gt;"</span><span style="color: #0000FF;">)</span>
case '\n' then puts(1,"</td></tr>\n<tr><td>")
<span style="color: #008080;">case</span> <span style="color: #008000;">','</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&lt;/td&gt;&lt;td&gt;"</span><span style="color: #0000FF;">)</span>
case ',' then puts(1,"</td><td>")
<span style="color: #008080;">case</span> <span style="color: #008000;">'&lt;'</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&amp;lt;"</span><span style="color: #0000FF;">)</span>
case '<' then puts(1,"&lt;")
<span style="color: #008080;">case</span> <span style="color: #008000;">'&gt;'</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&amp;gt;"</span><span style="color: #0000FF;">)</span>
case '>' then puts(1,"&gt;")
<span style="color: #008080;">case</span> <span style="color: #008000;">'&'</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&amp;amp;"</span><span style="color: #0000FF;">)</span>
case '&' then puts(1,"&amp;")
<span style="color: #008080;">case</span> <span style="color: #008080;">else</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">input</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
case else puts(1,input[i])
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
end switch
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"&lt;/td&gt;&lt;/tr&gt;\n&lt;/table&gt;"</span><span style="color: #0000FF;">)</span>
puts(1,"</td></tr>\n</table>")</lang>
<!--</syntaxhighlight>-->
{{out}}
<pre>
<lang html5><table>
&lt;table&gt;
<tr><td>Character</td><td>Speech</td></tr>
&lt;tr&gt;&lt;td&gt;Character&lt;/td&gt;&lt;td&gt;Speech&lt;/td&gt;&lt;/tr&gt;
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<&lt;tr><&gt;&lt;td>Brians&gt;The mother<multitude&lt;/td><td>&gt;&lt;angrytd&gt;NowThe you listen heremessiah! He'sShow notus the messiah; he's a very naughty boy! Now go away!&lt;/angrytd&gt;</td><&lt;/tr>&gt;
&lt;tr&gt;&lt;td&gt;Brians mother&lt;/td&gt;&lt;td&gt;&amp;lt;angry&amp;gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&amp;lt;/angry&amp;gt;&lt;/td&gt;&lt;/tr&gt;
<tr><td>The multitude</td><td>Who are you?</td></tr>
&lt;tr&gt;&lt;td&gt;The multitude&lt;/td&gt;&lt;td&gt;Who are you?&lt;/td&gt;&lt;/tr&gt;
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<&lt;tr><&gt;&lt;td>The&gt;Brians multitude<mother&lt;/td><&gt;&lt;td>Behold&gt;I'm his mother!; Beholdthat's his motherwho!<&lt;/td><&gt;&lt;/tr>&gt;
&lt;tr&gt;&lt;td&gt;The multitude&lt;/td&gt;&lt;td&gt;Behold his mother! Behold his mother!&lt;/td&gt;&lt;/tr&gt;
</table></lang>
&lt;/table&gt;</pre>
 
=={{header|PHP}}==
Line 4,014 ⟶ 4,422:
 
echo convert($csv);
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
===Simple solution===
<langsyntaxhighlight lang=PicoLisp>(load "@lib/http.l")
 
(in "text.csv"
Line 4,025 ⟶ 4,433:
(while (split (line) ",")
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @))))
(prinl) ) ) )</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang=html5><table class="myStyle">
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 4,034 ⟶ 4,442:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
===Extra credit solution===
<langsyntaxhighlight lang=PicoLisp>(load "@lib/http.l")
 
(in "text.csv"
Line 4,045 ⟶ 4,453:
(while (split (line) ",")
(<row> NIL (ht:Prin (pack (car @))) (ht:Prin (pack (cadr @))))
(prinl) ) ) ) )</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang=html5><table class="myStyle"><tr><th>Character</th><th>Speech</th></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td></tr>
Line 4,053 ⟶ 4,461:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|PowerShell}}==
===Simple solution===
<langsyntaxhighlight lang=Powershell>
Import-Csv -Path .\csv_html_test.csv | ConvertTo-Html -Fragment | Out-File .\csv_html_test.html
</syntaxhighlight>
</lang>
{{Out}}
<langsyntaxhighlight lang=html5><table>
<colgroup>
<col/>
Line 4,073 ⟶ 4,481:
<tr><td>Brians mother</td><td>I'm his mother; that's who! </td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother! </td></tr>
</table></langsyntaxhighlight>
===Extra credit solution===
<langsyntaxhighlight lang=Powershell>
$htmlformat = '<title>Csv to Html</title>'
$htmlformat += '<style type="text/css">'
Line 4,085 ⟶ 4,493:
Import-Csv -Path .\csv_html_test.csv | ConvertTo-Html -Head $htmlformat -Body '<h1>Csv to Html</h1>' | Out-File .\csv_html_test.html
Invoke-Expression .\csv_html_test.html
</syntaxhighlight>
</lang>
{{Out}}
<langsyntaxhighlight lang=html5>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
Line 4,109 ⟶ 4,517:
</table>
</body></html>
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Line 4,115 ⟶ 4,523:
{{Works with|SWI-Prolog}}
===Simple solution===
<langsyntaxhighlight lang=Prolog>csv_html :-
L = "Character,Speech
The multitude,The messiah! Show us the messiah!
Line 4,168 ⟶ 4,576:
csv_td_in(T, S).
 
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang=html5><TABLE>
<TR>
<TD>Character</TD><TD>Speech</TD>
Line 4,190 ⟶ 4,598:
</TR>
</TABLE>
</syntaxhighlight>
</lang>
===Extra credit solution===
<langsyntaxhighlight lang=Prolog>csv_html_plus :-
L =
"Character,Speech
Line 4,282 ⟶ 4,690:
[H],
csv_body_td_in(T, S).
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang=html5><TABLE>
<THEAD>
<TR>
Line 4,308 ⟶ 4,716:
</TBODY>
</TABLE>
</syntaxhighlight>
</lang>
 
===HTML outputs rendered in firefox browser===
Line 4,318 ⟶ 4,726:
(Note: rendered versions of all three outputs are shown at the foot of this section).
===Simple solution===
<langsyntaxhighlight lang=python>csvtxt = '''\
Character,Speech
The multitude,The messiah! Show us the messiah!
Line 4,345 ⟶ 4,753:
 
htmltxt = csv2html(csvtxt)
print(htmltxt)</langsyntaxhighlight>
 
'''Sample HTML output'''
 
<langsyntaxhighlight lang=html5><TABLE summary="csv2html program output">
<TBODY><TR><TD>Character</TD><TD>Speech</TD></TR></TBODY>
<TBODY><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY>
Line 4,356 ⟶ 4,764:
<TBODY><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY>
<TBODY><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY>
</TABLE></langsyntaxhighlight>
 
===Extra credit solution===
<langsyntaxhighlight lang=python>def _row2trextra(row, attr=None):
cols = escape(row).split(',')
attr_tr = attr.get('TR', '')
Line 4,388 ⟶ 4,796:
)
)
print(htmltxt)</langsyntaxhighlight>
 
'''Sample HTML output'''
Line 4,394 ⟶ 4,802:
The raw HTML would not render correctly through the wiki interface but shows a suitably coloured table with cell borders.
-->
<langsyntaxhighlight lang=html5><TABLE border="1" summary="csv2html extra program output">
<THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY>
Line 4,401 ⟶ 4,809:
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY>
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY>
</TABLE></langsyntaxhighlight>
 
===Robust solution===
Line 4,413 ⟶ 4,821:
Since the version of ElementTree in the standard library does not support pretty-printing, the output it produces is minified. Unlike the "extra credit" version, this doesn't put each <code>&lt;TR&gt;</code> element in its own <code>&lt;TBODY&gt;</code>.
 
<langsyntaxhighlight lang=python>from csv import DictReader
from xml.etree import ElementTree as ET
 
Line 4,449 ⟶ 4,857:
})
 
print(htmltxt.decode('utf8'))</langsyntaxhighlight>
 
'''Sample HTML output'''
Line 4,456 ⟶ 4,864:
The only difference between this and The output is semantically identical to the "extra credit" version, but whitespace has been collapsed as if it had been run through a minifier.
-->
<langsyntaxhighlight lang=html5><TABLE border="1" summary="csv2html extra program output"><THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD><TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR><TR><TD>Brians mother</TD><TD>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</TD></TR><TR><TD>The multitude</TD><TD>Who are you?</TD></TR><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY></TABLE></langsyntaxhighlight>
 
===HTML outputs rendered in firefox browser===
Line 4,464 ⟶ 4,872:
 
Using base R functions only, this is a very basic implementation and produces a simple HTML table
<langsyntaxhighlight lang=rsplus>File <- "~/test.csv"
Opened <- readLines(con = File)
Size <- length(Opened)
Line 4,483 ⟶ 4,891:
Table[length(Table)] <- paste0(Table[length(Table)],"</table>")
 
writeLines(as.character(Table), HTML)</langsyntaxhighlight>
 
'''Sample HTML output:'''
<langsyntaxhighlight lang=html5><table><tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr></table></langsyntaxhighlight>
 
=={{header|Racket}}==
 
Uses X-exprs:
<langsyntaxhighlight lang=racket>#lang racket
 
(define e.g.-CSV
Line 4,523 ⟶ 4,931:
 
(require xml)
(display (xexpr->string (CSV-lines->HTML-table e.g.-CSV)))</langsyntaxhighlight>
 
'''Sample HTML output:'''
 
<langsyntaxhighlight lang=html5><table><thead><tr><td>Character</td><td>Speech</td>
</tr></thead><tbody><tr><td>The multitude</td><td>The messiah! Show us the messiah!</td>
</tr><tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td>
Line 4,533 ⟶ 4,941:
</tr><tr><td>Brians mother</td><td>I'm his mother; that's who!</td>
</tr><tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td>
</tr></tbody></table></langsyntaxhighlight>
 
=={{header|Raku}}==
Line 4,539 ⟶ 4,947:
{{works with|rakudo|2015.09}}
A very lispy solution:
<syntaxhighlight lang=raku perl6line>my $str = "Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
Line 4,566 ⟶ 4,974:
# all whitespace and comments between split(…) and .map
.map({tag 'td', $^cell}))})\ # map those cells as td
.join("\n"); # append a newline for nicer output</langsyntaxhighlight>
 
{{Out}}
<langsyntaxhighlight lang=html><!DOCTYPE html>
<html>
<head><title>Some Text</title></head>
Line 4,579 ⟶ 4,987:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></body></html></langsyntaxhighlight>
 
=={{header|Red}}==
<langsyntaxhighlight lang=Red>Red []
 
csv: {Character,Speech
Line 4,621 ⟶ 5,029:
print csv2html csv ;; call function
write %data.html csv2html csv ;; write to file
</syntaxhighlight>
</lang>
output
<pre>
Line 4,644 ⟶ 5,052:
 
=={{header|Retro}}==
<langsyntaxhighlight lang=Retro>remapping off
"Character,Speech
The multitude,The messiah! Show us the messiah!
Line 4,666 ⟶ 5,074:
"</td></tr>\n</table>" puts ;
 
CSV displayHTML</langsyntaxhighlight>
 
{{Out}}
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 4,676 ⟶ 5,084:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|REXX}}==
Line 4,682 ⟶ 5,090:
:::* &nbsp; file:///c:/output.html
:::* &nbsp; file:///c:/outputh.html
<langsyntaxhighlight lang=rexx>/*REXX program converts CSV ───► HTML table representing the CSV data. */
arg header_ . /*obtain an uppercase version of args. */
wantsHdr= (header_=='HEADER') /*is the arg (low/upp/mix case)=HEADER?*/
Line 4,717 ⟶ 5,125:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
write: call lineout oFID, left('', 0 || arg(1) )arg(2); return</langsyntaxhighlight>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]]. <br>
 
Line 4,826 ⟶ 5,234:
ruby csv2html.rb header
I/O is done through standard input/output.
<langsyntaxhighlight lang=ruby>require 'cgi'
 
puts '<table summary="csv2html program output">'
Line 4,842 ⟶ 5,250:
end
 
puts "</table>"</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang=html5><table summary="csv2html program output">
<tr><th>Character</th><th>Speech</th></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 4,851 ⟶ 5,259:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Run BASIC}}==
The extra credit version has 2 extra lines of code to get the heading.
<langsyntaxhighlight lang=rnbasic>csv$ = "Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
Line 4,878 ⟶ 5,286:
k = instr(strRep$,rep$)
WEND
END FUNCTION</langsyntaxhighlight>
{{Out}}
<table border=1><TR bgcolor=wheat align=center><th>Character</th><th>Speech</th></TR>
Line 4,889 ⟶ 5,297:
=={{header|Rust}}==
{{trans|C}}
<langsyntaxhighlight lang=rust>static INPUT : &'static str =
"Character,Speech
The multitude,The messiah! Show us the messiah!
Line 4,911 ⟶ 5,319:
println!("</td></tr>\n</table>");
}
</syntaxhighlight>
</lang>
{{Out}}
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 4,920 ⟶ 5,328:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Scala}}==
{{libheader|org.scala-lang.xml}}
Scala has built-in support for XML, so you can freely mix XML literals into your Scala source code. This is nice, because instead of using strings to represent XML, you create XML literals that the compiler can understand and verify. This approach lets you easily generate dynamic XML by interweaving Scala code and XML in the same expressions.<langsyntaxhighlight lang=scala>object CsvToHTML extends App {
val header = <head>
<title>CsvToHTML</title>
Line 4,965 ⟶ 5,373:
 
println(csv2html(csv, true))
}</langsyntaxhighlight>{{out}}<syntaxhighlight lang =html><html>
<head>
<title>CsvToHTML</title>
Line 5,014 ⟶ 5,422:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|Sed}}==
 
File csv2html.sed
<langsyntaxhighlight lang=sed>#!/bin/sed -f
 
s|<|\&lt;|g
Line 5,027 ⟶ 5,435:
s|$|</td>\n </tr>|
1s|^|<table>\n|
$s|$|\n</table>|</langsyntaxhighlight>
 
<pre>$ sed -f csv2html.sed input.csv</pre>
 
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 5,056 ⟶ 5,464:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 5,062 ⟶ 5,470:
function [http://seed7.sourceforge.net/libraries/html_ent.htm#encodeHtmlContent%28in_string%29 encodeHtmlContent],
which replaces characters with HTML entities. E.g.: '<' is replaced by ''&amp;lt;''.
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
include "html_ent.s7i";
 
Line 5,091 ⟶ 5,499:
end for;
writeln("</table>");
end func;</langsyntaxhighlight>
 
{{Out}}
<langsyntaxhighlight lang=html5>
<table>
<tr><th>Character</th><th>Speech</th></tr>
Line 5,104 ⟶ 5,512:
<tr><td></td></tr>
</table>
</syntaxhighlight>
</lang>
 
{{Out}} viewed with a browser:
Line 5,118 ⟶ 5,526:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang=ruby>func escape(str) { str.trans(« & < > », « &amp; &lt; &gt; ») }
func tag(t, d) { "<#{t}>#{d}</#{t}>" }
 
Line 5,147 ⟶ 5,555:
EOT
 
print csv2html(str)</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><!DOCTYPE html>
<html>
<head><title>Some Text</title></head>
Line 5,159 ⟶ 5,567:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></body></html></langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 5,165 ⟶ 5,573:
{{tcllib|html}}
{{tcllib|struct::queue}}
<langsyntaxhighlight lang=tcl>package require Tcl 8.5
package require csv
package require html
Line 5,188 ⟶ 5,596:
}]
[html::closeTag]
}]</langsyntaxhighlight>
 
Extra credit version:
<langsyntaxhighlight lang=tcl>package require Tcl 8.5
package require csv
package require html
Line 5,235 ⟶ 5,643:
}]
}]
}]</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang=html5><table border="1" summary="csv2html program output">
<tr bgcolor="yellow"><td>Character</td><td>Speech</td></tr>
<tr bgcolor="orange"><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
Line 5,244 ⟶ 5,652:
<tr bgcolor="orange"><td>Brians mother</td><td>I&#39;m his mother; that&#39;s who!</td></tr>
<tr bgcolor="orange"><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang=tuscript>
$$ MODE TUSCRIPT
MODE DATA
Line 5,286 ⟶ 5,694:
WRITE html "</table></body></html>"
ENDACCESS/PRINT html
</syntaxhighlight>
</lang>
=== Output (source code) ===
<langsyntaxhighlight lang=html5><!DOCTYPE html system>
<html>
<head>
Line 5,303 ⟶ 5,711:
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></body></html>
</syntaxhighlight>
</lang>
=== Output (rendered) ===
[[file:tuscript_csv2html.png|500px|thumb|none|rendered by browser ]]
Line 5,311 ⟶ 5,719:
====Simple====
 
<langsyntaxhighlight lang=txr>@(collect)
@char,@speech
@(end)
Line 5,323 ⟶ 5,731:
@ (end)
</table>
@(end)</langsyntaxhighlight>
 
{{Out}}
<pre>$ txr csv.txr csv.txt</pre>
 
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 5,353 ⟶ 5,761:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
====With Styling====
 
<langsyntaxhighlight lang=txr>@(collect)
@char,@speech
@(end)
Line 5,385 ⟶ 5,793:
@ (end)
</table>
@(end)</langsyntaxhighlight>
 
{{Out}}
<pre>$ txr csv2.txr csv.txt</pre>
<langsyntaxhighlight lang=html5><style type="text/css">
tr.odd td {
background-color: #CC9999; color: black;
Line 5,425 ⟶ 5,833:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|bash}}
<syntaxhighlight lang=bash>csv2html() {
{{works with|ksh}}
<lang bash>csv2html() {
IFS=,
echo "<table>"
 
echo "<thead>"
read -ra speaker textfields
htmlrow "$speaker"th "$text{fields[@]}" th
echo "</thead>"
 
echo "<tbody>"
while read -ra speaker text; dofields
do htmlrow "$speaker"td "$text{fields[@]}"
done
echo "</tbody>"
echo "</table>"
}
 
htmlrow() {
cell=${3:-td}1
shift
printf "<tr><%s>%s</%s><%s>%s</%s></tr>\n" \
echo "<tr>"
"$cell" "$(escape_html "$1")" "$cell" \
for field
"$cell" "$(escape_html "$2")" "$cell"
do echo "<$cell>$(escape_html "$field")</$cell>"
done
echo "</tr>"
}
 
escape_html() {
str=${1//\&/&amp;}
Line 5,461 ⟶ 5,871:
}
 
csv2html <<-END
html=$(
Character,Speech
csv2html <<-END
The multitude,The messiah! Show us the messiah!
Character,Speech
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,The messiah! Show us the messiah!
The multitude,Who are you?
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
Brians mother,I'm his mother; that's who!
The multitude,Who are you?
BriansThe mothermultitude,I'mBehold his mother;! that'sBehold whohis mother!
END</syntaxhighlight>
The multitude,Behold his mother! Behold his mother!
END
)
echo "$html"</lang>
 
{{output}}
<langsyntaxhighlight lang=html5><table>
<thead>
<tr>
<tr><th>Character</th><th>Speech</th></tr>
<th>Character</th>
<th>Speech</th>
</tr>
</thead>
<tbody>
<tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<td>The multitude</td>
<tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td></tr>
<tr><td>The multitude</td><td>Whomessiah! areShow you?us the messiah!</td></tr>
</tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
<td>Brians mother</td>
<td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td>
</tr>
<tr>
<td>The multitude</td>
<td>Who are you?</td>
</tr>
<tr>
<td>Brians mother</td>
<td>I'm his mother; that's who!</td>
</tr>
<tr>
<td>The multitude</td>
<td>Behold his mother! Behold his mother!</td>
</tr>
</tbody>
</table></langsyntaxhighlight>
 
=={{header|VBA}}==
{{trans|Phix}}<langsyntaxhighlight lang=vb>Public Sub CSV_TO_HTML()
input_ = "Character,Speech\n" & _
"The multitude,The messiah! Show us the messiah!\n" & _
Line 5,515 ⟶ 5,940:
Next i
Debug.Print "</td></tr>" & vbCrLf & "</table>"
End Sub</langsyntaxhighlight>{{out}}
<langsyntaxhighlight lang=html5><table>
<tr><td>
Character</td><td>Speech</td></tr>
Line 5,524 ⟶ 5,949:
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|VBScript}}==
<syntaxhighlight lang=vb>
<lang vb>
Set objfso = CreateObject("Scripting.FileSystemObject")
 
Line 5,561 ⟶ 5,986:
replace_chars = Replace(Replace(s,"<","&lt;"),">","&gt;")
End Function
</syntaxhighlight>
</lang>
 
{{out}}
Line 5,572 ⟶ 5,997:
If a block is highlighted, only the block is converted. If no block highlighted, the entire file is converted.
 
<langsyntaxhighlight lang=vedit>if (BB < 0) { // block not set
BB(0) // convert entire file
BE(File_Size)
Line 5,598 ⟶ 6,023:
Line(1)
}
BB(Clear)</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr><td>Character</td><td>Speech </td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah! </td></tr>
Line 5,608 ⟶ 6,033:
<tr><td>Brians mother</td><td>I'm his mother; that's who! </td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table></langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
Line 5,619 ⟶ 6,044:
TextFieldParser is designed to work with files and so makes heavy use of peeking, which results in buggy behavior when signaling end-of-file using the console. The most reliable way seems to be alternately pressing enter and Ctrl+Z after the last character of the last line of data.
 
<langsyntaxhighlight lang=vbnet>Imports Microsoft.VisualBasic.FileIO
 
Module Program
Line 5,661 ⟶ 6,086:
</table>
End Function
End Module</langsyntaxhighlight>
 
{{out|input=true}}
<langsyntaxhighlight lang=html5>Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
Line 5,697 ⟶ 6,122:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="wren">var csv =
"Character,Speech\n" +
"The multitude,The messiah! Show us the messiah!\n" +
"Brians mother,<angry>Now you listen here! He's not the messiah; " +
"he's a very naughty boy! Now go away!</angry>\n" +
"The multitude,Who are you?\n" +
"Brians mother,I'm his mother; that's who!\n" +
"The multitude,Behold his mother! Behold his mother!"
 
var i = " " // indent
var ii = i + i // double indent
var iii = ii + i // triple indent
var sb = "<table>\n%(i)<tr>\n%(ii)<td>"
for (c in csv) {
sb = sb + ((c == "\n") ? "</td>\n%(i)</tr>\n%(i)<tr>\n%(ii)<td>" :
(c == ",") ? "</td>\n%(ii)<td>" :
(c == "&") ? "&amp;" :
(c == "'") ? "&apos;" :
(c == "<") ? "&lt;" :
(c == ">") ? "&gt;" : c)
}
sb = sb + "</td>\n%(i)</tr>\n</table>"
System.print(sb)
System.print()
 
// now using first row as a table header
sb = "<table>\n%(i)<thead>\n%(ii)<tr>\n%(iii)<td>"
var hLength = csv.indexOf("\n") + 1 // find length of first row including CR
for (c in csv.take(hLength)) {
sb = sb + ((c == "\n") ? "</td>\n%(ii)</tr>\n%(i)</thead>\n%(i)<tbody>\n%(ii)<tr>\n%(iii)<td>" :
(c == ",") ? "</td>\n%(iii)<td>" : c)
}
for (c in csv.skip(hLength)) {
sb = sb + ((c == "\n") ? "</td>\n%(ii)</tr>\n%(ii)<tr>\n%(iii)<td>" :
(c == ",") ? "</td>\n%(iii)<td>" :
(c == "&") ? "&amp;" :
(c == "'") ? "&apos;" :
(c == "<") ? "&lt;" :
(c == ">") ? "&gt;" : c)
}
sb = sb + "</td>\n%(ii)</tr>\n%(i)</tbody>\n</table>"
System.print(sb)</syntaxhighlight>
 
{{out}}
<pre>
Same as Kotlin entry.
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">string 0;
char Input, S;
[Input:=
"Character,Speech
The multitude,The messiah! Show us the messiah!
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
The multitude,Who are you?
Brians mother,I'm his mother; that's who!
The multitude,Behold his mother! Behold his mother!";
 
Text(0, "<table>^m^j<tr><td>");
S:= Input;
while S(0) do
[case S(0) of
$0A: Text(0, "</td></tr>^m^j<tr><td>");
^,: Text(0, "</td><td>");
^<: Text(0, "&lt;");
^>: Text(0, "&gt;");
^&: Text(0, "&amp;")
other ChOut(0, S(0));
S:= S+1;
];
Text(0, "</td></tr>^m^j</table>");
]</syntaxhighlight>
{{out}}
<pre>
<table>
<tr><td>Character</td><td>Speech</td></tr>
<tr><td>The multitude</td><td>The messiah! Show us the messiah!</td></tr>
<tr><td>Brians mother</td><td>&lt;angry&gt;Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!&lt;/angry&gt;</td></tr>
<tr><td>The multitude</td><td>Who are you?</td></tr>
<tr><td>Brians mother</td><td>I'm his mother; that's who!</td></tr>
<tr><td>The multitude</td><td>Behold his mother! Behold his mother!</td></tr>
</table>
</pre>
 
=={{header|XSLT 2.0}}==
Line 5,705 ⟶ 6,218:
# Pass as the URI of the input csv as a parameter (named url-of-csv) to your XSLT 2.0 processor
 
<syntaxhighlight lang=text><xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xcsvt="http://www.seanbdurkin.id.au/xslt/csv-to-xml.xslt"
Line 5,758 ⟶ 6,271:
<xsl:template match="xcsv:notice" mode="phase-2" />
 
</xsl:stylesheet></langsyntaxhighlight>
 
This style-sheet, when applied to the task input, yields...
 
<langsyntaxhighlight lang=html5><!DOCTYPE html
SYSTEM "about:legacy-compat">
<html lang="en">
Line 5,799 ⟶ 6,312:
</table>
</body>
</html></langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang=zkl>csvData:=Data(0,Int,"Character,Speech\n"
"The multitude,The messiah! Show us the messiah!\n"
"Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>\n"
Line 5,814 ⟶ 6,327:
.pump("<tr>\n","strip",String.fpM("101"," <td>","</td>\n"))+"</tr>\n"
}) + "</table>";
html.println();</langsyntaxhighlight>
The pump method writes or appends to a sink, in this case a string (eg "<table/>"). The fpM method is function/partial application and takes a string (ones and zeros) to indicate the positional input parameters. So String.fpM("101","a","c")("B") creates a new string "aBc".
{{out}}
<langsyntaxhighlight lang=html5><table>
<tr>
<td>Character</td>
Line 5,842 ⟶ 6,355:
<td>Behold his mother! Behold his mother!</td>
</tr>
</table></langsyntaxhighlight>
 
[[Category:CSV]]
[[Category:HTML]]
1,983

edits