1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
7 <meta name=
"Generator" content=
"Cocoa HTML Writer">
8 <meta name=
"CocoaVersion" content=
"949.43">
9 <style type=
"text/css">
10 p
.p1
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
}
11 p
.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; min-height: 14.0px}
12 p
.p3
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; color: #0000bf}
13 p
.p4
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #bf0000}
16 p
.p7
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #606060}
17 p
.p8
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #606060; min-height: 12.0px}
18 p
.p9
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #0000bf}
19 span
.s1
{font: 18.0px Helvetica
}
20 span
.s2
{color: #000000}
21 span
.s3
{color: #0000bf}
22 span
.s4
{color: #0000bf}
23 span
.s5
{color: #606060}
24 span
.s6
{color: #bf0000}
25 span
.Apple-tab-span
{white-space:pre
}
29 <p class=
"p1"><span class=
"s1"><b>CSVFileReader
</b></span><b> <span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>file reader for comma separated data
</b></p>
30 <p class=
"p2"><br></p>
31 <p class=
"p3"><span class=
"s2"><b>Inherits from:
</b><a href=
"../Core/Object.html"><b>Object
</b></a></span><b> </b><span class=
"s2"><b>:
</b><a href=
"../Core/Kernel/AbstractFunction.html"><b>AbstractFunction
</b></a></span><b> :
</b><a href=
"../Streams-Patterns-Events/Stream.html"><b>Stream
</b></a><b> :
</b><a href=
"FileReader.html"><b>FileReader
</b></a></p>
32 <p class=
"p2"><br></p>
33 <p class=
"p1"><b>CSVFileReader reads comma-separated text files into
2D arrays line by line.
</b></p>
34 <p class=
"p2"><br></p>
35 <p class=
"p1">For tab delimited files use:
<a href=
"TabFileReader.html"><span class=
"s3">TabFileReader
</span></a></p>
36 <p class=
"p1">For space-delimited files, or custom delimiters, use:
<a href=
"FileReader.html"><span class=
"s3">FileReader
</span></a></p>
37 <p class=
"p2"><br></p>
38 <p class=
"p2"><br></p>
39 <p class=
"p1"><b>*read(path, skipEmptyLines, skipBlanks, func)
</b></p>
40 <p class=
"p4"><br></p>
41 <p class=
"p4"><br></p>
43 <p class=
"p6">// write a test file:
</p>
44 <p class=
"p7"><span class=
"s2">f =
</span><span class=
"s4">File
</span><span class=
"s2">(
</span>"CSVReadTest.sc"<span class=
"s2">,
</span>"w"<span class=
"s2">);
</span></p>
45 <p class=
"p5">f.write(
</p>
46 <p class=
"p7">"Some,comma,delimited,items, in line 1</p>
47 <p class="p8
"><br></p>
48 <p class="p7
">and then, some more, with several commas,,,, in line 3</p>
51 <p class=
"p5">f.close;
<span class=
"Apple-converted-space"> </span></p>
53 <p class=
"p4"><br></p>
54 <p class=
"p4"><br></p>
55 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// open file, read and put strings into array, close file.
<span class=
"Apple-converted-space"> </span></p>
56 <p class=
"p5">x =
<span class=
"s4">CSVFileReader
</span>.read(
<span class=
"s5">"CSVReadTest.sc"</span>).postcs;
</p>
57 <p class=
"p4"><br></p>
58 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// can skip empty lines:
<span class=
"Apple-converted-space"> </span></p>
59 <p class=
"p5">x =
<span class=
"s4">CSVFileReader
</span>.read(
<span class=
"s5">"CSVReadTest.sc"</span>,
<span class=
"s4">true
</span>).postcs;
</p>
60 <p class=
"p4"><br></p>
61 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// can skip blank entries caused by multiple commas :
<span class=
"Apple-converted-space"> </span></p>
62 <p class=
"p5">x =
<span class=
"s4">CSVFileReader
</span>.read(
<span class=
"s5">"CSVReadTest.sc"</span>,
<span class=
"s4">true
</span>,
<span class=
"s4">true
</span>).postcs;
</p>
63 <p class=
"p4"><span class=
"Apple-tab-span"> </span></p>
64 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>// do file open/close by hand if you prefer:
</p>
65 <p class=
"p5">f =
<span class=
"s4">File
</span>(
<span class=
"s5">"CSVReadTest.sc"</span>,
<span class=
"s5">"r"</span>); f.isOpen;
</p>
66 <p class=
"p9"><span class=
"s2">t =
</span>CSVFileReader
<span class=
"s2">(f);
<span class=
"Apple-tab-span"> </span></span></p>
67 <p class=
"p5">t.read(
<span class=
"s4">true
</span>,
<span class=
"s4">true
</span>).postcs;
</p>
68 <p class=
"p5">f.close;
</p>
69 <p class=
"p4"><br></p>
71 <p class=
"p6">// write a test file with numbers:
</p>
72 <p class=
"p7"><span class=
"s2">f =
</span><span class=
"s4">File
</span><span class=
"s2">(
</span>"CSVReadTestNum.sc"<span class=
"s2">,
</span>"w"<span class=
"s2">);
</span></p>
73 <p class=
"p4"><br></p>
74 <p class=
"p5">(
1.
.10).do {
<span class=
"s4">|n|
</span> f.write(n.asString ++
<span class=
"s5">","</span>); };
</p>
75 <p class=
"p5">f.close;
<span class=
"Apple-converted-space"> </span></p>
77 <p class=
"p4"><br></p>
78 <p class=
"p5">x =
<span class=
"s4">CSVFileReader
</span>.read(
<span class=
"s5">"CSVReadTestNum.sc"</span>,
<span class=
"s4">true
</span>,
<span class=
"s4">true
</span>).postcs;
</p>
79 <p class=
"p5">x.collect(
<span class=
"s4">_
</span>.collect(
<span class=
"s4">_
</span>.interpret));
<span class=
"Apple-tab-span"> </span><span class=
"s6">// convert to numbers.
</span></p>
80 <p class=
"p4"><br></p>
81 <p class=
"p6"><span class=
"s2"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// or do it immediately:
</p>
82 <p class=
"p5">x =
<span class=
"s4">CSVFileReader
</span>.readInterpret(
<span class=
"s5">"CSVReadTestNum.sc"</span>).postcs;
</p>
83 <p class=
"p4"><br></p>
85 <p class=
"p6">// write a test file with several lines of numbers:
</p>
86 <p class=
"p7"><span class=
"s2">f =
</span><span class=
"s4">File
</span><span class=
"s2">(
</span>"CSVReadTestNum.sc"<span class=
"s2">,
</span>"w"<span class=
"s2">);
</span></p>
87 <p class=
"p4"><br></p>
88 <p class=
"p5">(
1.
.100).do {
<span class=
"s4">|n|
</span><span class=
"Apple-converted-space"> </span></p>
89 <p class=
"p5"><span class=
"Apple-tab-span"> </span>f.write(n.asString ++ if (n %
10 !=
0,
<span class=
"s5">","</span>,
<span class=
"s4">Char
</span>.nl)); };
</p>
90 <p class=
"p5">f.close;
<span class=
"Apple-converted-space"> </span></p>
92 <p class=
"p4"><br></p>
93 <p class=
"p4"><br></p>
94 <p class=
"p5">x =
<span class=
"s4">CSVFileReader
</span>.readInterpret(
<span class=
"s5">"CSVReadTestNum.sc"</span>,
<span class=
"s4">true
</span>,
<span class=
"s4">true
</span>).postln;
</p>
95 <p class=
"p4"><br></p>