| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<title> |
| 5 |
lazy-csv: fast, space-efficient, CSV parsing |
| 6 |
</title> |
| 7 |
</head> |
| 8 |
|
| 9 |
<body bgcolor='#ffffff'> |
| 10 |
|
| 11 |
<center> |
| 12 |
<h1>lazy-csv</h1> |
| 13 |
<table><tr><td width=200 align=center> |
| 14 |
<a href="#what">What is lazy-csv?</a><br> |
| 15 |
<a href="#tool">Cmdline tool: csvSelect</a><br> |
| 16 |
<a href="#download">Downloads</a><br> |
| 17 |
</td><td width=200 align=center> |
| 18 |
<a href="#news">Recent news</a><br> |
| 19 |
<a href="#who">Contacts</a><br> |
| 20 |
<a href="#related">Related Work</a><br> |
| 21 |
</td></tr></table> |
| 22 |
</center> |
| 23 |
|
| 24 |
<hr> |
| 25 |
<center><h3><a name="what">What is lazy-csv?</a></h3></center> |
| 26 |
<p> |
| 27 |
<b>lazy-csv</b> is a library in Haskell, for reading CSV |
| 28 |
(comma-separated value) data. It is lazier, faster, more |
| 29 |
space-efficient, and more flexible in its treatment of errors, than any |
| 30 |
other extant Haskell CSV library on Hackage. |
| 31 |
|
| 32 |
<p> |
| 33 |
<a href="haddock/index.html">Detailed documentation of the lazy-csv API</a> |
| 34 |
is generated automatically by Haddock directly from the source code. |
| 35 |
|
| 36 |
<p> |
| 37 |
You can choose between String and ByteString variants of the API, just |
| 38 |
by picking the appropriate import. The API is identical modulo the |
| 39 |
type. Here is an example program: |
| 40 |
|
| 41 |
<pre style="font-family:Consolas, Monaco, Monospace;"><span style="color: #00ff00;text-decoration: underline;">module</span><span style=""> </span><span style="">Main</span><span style=""> </span><span style="color: #00ff00;text-decoration: underline;">where</span><span style=""> |
| 42 |
</span><span style=""> |
| 43 |
</span><span style="color: #00ff00;text-decoration: underline;">import</span><span style=""> </span><span style="">Text</span><span style="color: #00ffff;">.</span><span style="">CSV</span><span style="color: #00ffff;">.</span><span style="">Lazy</span><span style="color: #00ffff;">.</span><span style="">String</span><span style=""> |
| 44 |
</span><span style="color: #00ff00;text-decoration: underline;">import</span><span style=""> </span><span style="">System</span><span style=""> |
| 45 |
</span><span style=""> |
| 46 |
</span><span style="color: #0000ff;font-style: italic;">-- read a CSV file, select the 3rd column, and print it out again.</span><span style=""> |
| 47 |
</span><span style=""> |
| 48 |
</span><span style="color: #0000ff;">main</span><span style=""> </span><span style="color: #ff0000;">=</span><span style=""> </span><span style="color: #00ff00;text-decoration: underline;">do</span><span style=""> |
| 49 |
</span><span style=""> </span><span style="color: #ff0000;">[</span><span style="">file</span><span style="color: #ff0000;">]</span><span style=""> </span><span style="color: #ff0000;"><-</span><span style=""> </span><span style="">getArgs</span><span style=""> |
| 50 |
</span><span style=""> </span><span style="">content</span><span style=""> </span><span style="color: #ff0000;"><-</span><span style=""> </span><span style="">readFile</span><span style=""> </span><span style="">file</span><span style=""> |
| 51 |
</span><span style=""> </span><span style="color: #00ff00;text-decoration: underline;">let</span><span style=""> </span><span style="">csv</span><span style=""> </span><span style="color: #ff0000;">=</span><span style=""> </span><span style="">parseCSV</span><span style=""> </span><span style="">content</span><span style=""> |
| 52 |
</span><span style=""> </span><span style="color: #00ff00;text-decoration: underline;">case</span><span style=""> </span><span style="">csvErrors</span><span style=""> </span><span style="">csv</span><span style=""> </span><span style="color: #00ff00;text-decoration: underline;">of</span><span style=""> |
| 53 |
</span><span style=""> </span><span style="">errs</span><span style="color: #ff0000;">@</span><span style="color: #00ffff;">(</span><span style="color: #00ff00;text-decoration: underline;">_</span><span style="color: #ff0000;font-weight: bold;">:</span><span style="color: #00ff00;text-decoration: underline;">_</span><span style="color: #00ffff;">)</span><span style=""> </span><span style="color: #ff0000;">-></span><span style=""> </span><span style="">print</span><span style=""> </span><span style="color: #00ffff;">(</span><span style="">unlines</span><span style=""> </span><span style="color: #00ffff;">(</span><span style="">map</span><span style=""> </span><span style="">ppCSVError</span><span style=""> </span><span style="">errs</span><span style="color: #00ffff;">)</span><span style="color: #00ffff;">)</span><span style=""> |
| 54 |
</span><span style=""> </span><span style="">[]</span><span style=""> </span><span style="color: #ff0000;">-></span><span style=""> </span><span style="color: #00ff00;text-decoration: underline;">do</span><span style=""> </span><span style="">content</span><span style=""> </span><span style="color: #ff0000;"><-</span><span style=""> </span><span style="">readFile</span><span style=""> </span><span style="">file</span><span style=""> |
| 55 |
</span><span style=""> </span><span style="color: #00ff00;text-decoration: underline;">let</span><span style=""> </span><span style="">selection</span><span style=""> </span><span style="color: #ff0000;">=</span><span style=""> </span><span style="">map</span><span style=""> </span><span style="color: #00ffff;">(</span><span style="">take</span><span style=""> </span><span style="color: #ff00ff;">1</span><span style=""> </span><span style="color: #00ffff;">.</span><span style=""> </span><span style="">drop</span><span style=""> </span><span style="color: #ff00ff;">2</span><span style="color: #00ffff;">)</span><span style=""> |
| 56 |
</span><span style=""> </span><span style="color: #00ffff;">(</span><span style="">csvTable</span><span style=""> </span><span style="color: #00ffff;">(</span><span style="">parseCSV</span><span style=""> </span><span style="">content</span><span style="color: #00ffff;">)</span><span style="color: #00ffff;">)</span><span style=""> |
| 57 |
</span><span style=""> </span><span style="">putStrLn</span><span style=""> </span><span style="color: #00ffff;">$</span><span style=""> </span><span style="">ppCSVTable</span><span style=""> </span><span style="">selection</span><span style=""> |
| 58 |
</span></pre> |
| 59 |
|
| 60 |
<p> |
| 61 |
There are two useful things to note about the API, arising out of this |
| 62 |
example. First, <tt>parseCSV</tt> does not directly give you the value |
| 63 |
of the CSV table, but rather gives a <tt>CSVResult</tt>. You must |
| 64 |
project out either the errors (with <tt>csvErrors</tt>) or the values |
| 65 |
(with <tt>csvTable</tt>). Secondly, because the result of |
| 66 |
<tt>parseCSV</tt> is lazy, it is in fact more space-efficient (and also |
| 67 |
faster) to get hold of the valid table contents by <em>reopening and |
| 68 |
reparsing</em> the file after checking for errors. This also means, of |
| 69 |
course, that you can simply omit the step of checking for errors and |
| 70 |
ignore them if you wish. |
| 71 |
|
| 72 |
<p> |
| 73 |
To illustrate the performance of lazy-csv, here is a micro-benchmark. |
| 74 |
We compare the same program (the example above) recoded |
| 75 |
with all of the CSV libraries available on Hackage. The libraries are: |
| 76 |
|
| 77 |
<center> |
| 78 |
<table border=1 cellspacing=0 cellpadding=2> |
| 79 |
<tr><td><em>library</em></td><td><em>string type</em></td><td><em>parsing/lexing</em></td><td><em>results</em></td><td><em>error-reporting</em></td></tr> |
| 80 |
<tr><td>csv</td><td>String</td><td>Parsec</td><td>strict</td><td>first error</td></tr> |
| 81 |
<tr><td>bytestring-csv</td><td>ByteString</td><td>Alex</td><td>strict</td><td>Nothing (Maybe)</td></tr> |
| 82 |
<tr><td>spreadsheet</td><td>String</td><td>custom parser</td><td>lazy</td><td>first error</td></tr> |
| 83 |
<tr><td>lazy-csv</td><td>String</td><td>custom lexer</td><td>lazy</td><td>all errors</td></tr> |
| 84 |
<tr><td>lazy-csv</td><td>ByteString</td><td>custom lexer</td><td>lazy</td><td>all errors</td></tr> |
| 85 |
<tr><td>lazy-csv</td><td>ByteString</td><td>custom lexer</td><td>lazy</td><td>discarding errors</td></tr> |
| 86 |
</table> |
| 87 |
</center> |
| 88 |
|
| 89 |
<p> |
| 90 |
The main differences are shown in the table. As far as error-reporting |
| 91 |
goes, The Parsec-based CSV parser will report only the first error |
| 92 |
encountered. The Alex-based bytestring-csv will stop at the first |
| 93 |
error, but not give you any information about it. The Spreadsheet library |
| 94 |
has a lazy parser, allowing you to retrieve the initial portion of valid |
| 95 |
data, as far as the first error. The lazy-csv library will notify all |
| 96 |
errors in the input in addition to returning all the well-formed data it |
| 97 |
can find. Many of the possible CSV formatting errors are easily |
| 98 |
recoverable, such as incorrect number of fields in a row, bad quoting, etc. |
| 99 |
Thus, with the lazy-csv library one can choose to halt on errors, or display |
| 100 |
them as warnings whilst continuing with good data, or ignore the errors |
| 101 |
completely, continuing to process the retrievable data. |
| 102 |
|
| 103 |
<p> |
| 104 |
The choice of lazy vs strict input is extremely important when it comes |
| 105 |
to large file sizes. |
| 106 |
Here are some indicative performance figures, using as input a series |
| 107 |
of files of increasing size: 1Mb, 10Mb, 100Mb, 1Gb. For the purposes of |
| 108 |
comparison, I include three sets of numbers for the lazy library - two |
| 109 |
with error-reporting using each of String and ByteString types, the |
| 110 |
third for ByteString but ignoring errors. In all cases the good data |
| 111 |
is processed anyway, but the difference in reporting leads to |
| 112 |
significant performance differences. |
| 113 |
|
| 114 |
<p> |
| 115 |
Finally, the nearest non-Haskell comparison I could think of is to use |
| 116 |
the Unix tool 'cut'. Of course, 'cut' (with comma as delimiter) is not |
| 117 |
a correct CSV-parser, but it does have the benefit of simplicity, and |
| 118 |
most closely resembles the lazy-csv library in terms of ignoring errors |
| 119 |
and continuing with good data. It also has lazy streaming behaviour on |
| 120 |
very large files. |
| 121 |
|
| 122 |
<center> |
| 123 |
<table border=1 cellspacing=0 cellpadding=2> |
| 124 |
<tr><td><em>library</em></td><td><em>1Mb</em></td><td><em>10Mb</em></td><td><em>100Mb</em></td><td><em>1Gb</em></td></tr> |
| 125 |
<tr><td>spreadsheet</td><td>runtime failure</td><td>runtime failure</td><td>runtime failure</td><td>runtime failure</td></tr> |
| 126 |
<tr><td>csv</td><td>0.542</td><td>20.483</td><td>stack overflow</td><td>stack overflow</td></tr> |
| 127 |
<tr><td>bytestring-csv</td><td>0.273</td><td>2.656</td><td>27.187</td><td>out of memory</td></tr> |
| 128 |
<tr><td>lazy-csv (String)</td><td>0.196</td><td>1.890</td><td>18.845</td><td>189.978</td></tr> |
| 129 |
<tr><td>lazy-csv (ByteString)</td><td>0.148</td><td>1.399</td><td>13.936</td><td>139.379</td></tr> |
| 130 |
<tr><td>lazy-csv (ByteString, discard errors)</td><td>0.087</td><td>0.817</td><td>8.102</td><td>80.835</td></tr> |
| 131 |
<tr><td>cut -d',' -f3</td><td>0.052</td><td>0.462</td><td>4.576</td><td>45.726</td></tr> |
| 132 |
</table> |
| 133 |
</center> |
| 134 |
|
| 135 |
<p> |
| 136 |
All timings are in seconds, measured best-of-3 with the unix time |
| 137 |
command, on a 2.26Gz Intel Core 2 Duo MacBook with 4Gb RAM, compiled |
| 138 |
with ghc-6.10.4 using -O optimisation. |
| 139 |
|
| 140 |
<p> |
| 141 |
How much maximum live heap do these implementations use, for different |
| 142 |
input sizes? (All measured from ghc heap profiles.) |
| 143 |
|
| 144 |
<center> |
| 145 |
<table border=1 cellspacing=0 cellpadding=2> |
| 146 |
<tr><td><em>library</em></td><td><em>1Mb</em></td><td><em>10Mb</em></td><td><em>100Mb</em></td><td><em>1Gb</em></td></tr> |
| 147 |
<tr><td>csv</td><td>8Mb</td><td>120Mb</td><td>stack overflow</td><td>stack overflow</td></tr> |
| 148 |
<tr><td>bytestring-csv</td><td>empty profile</td><td>52Mb</td><td>700Mb</td><td>out of memory</td></tr> |
| 149 |
<tr><td>lazy-csv (String)</td><td>12kb</td><td>12kb</td><td>12kb</td><td>12kb</td></tr> |
| 150 |
<tr><td>lazy-csv (ByteString)</td><td>3kb</td><td>3kb</td><td>3kb</td><td>3kb</td></tr> |
| 151 |
</table> |
| 152 |
</center> |
| 153 |
|
| 154 |
<p> |
| 155 |
My conclusions are these: |
| 156 |
<ul> |
| 157 |
<li>If you want good performance that scales across very large inputs, make |
| 158 |
sure you use lazy I/O. |
| 159 |
<li>Lazy Strings can be faster, and scale better, than strict ByteStrings. |
| 160 |
<li>This is mainly because a hand-written lexer can be significantly |
| 161 |
faster than machine-generated lexers. |
| 162 |
<li>Lazy ByteStrings perform best of all (in Haskell). |
| 163 |
<li>A correct CSV parser need not be much slower than a fast incorrect |
| 164 |
one (like unix cut). |
| 165 |
<li>Good error-handling is not in any way detrimental to performance. |
| 166 |
</ul> |
| 167 |
|
| 168 |
<hr> |
| 169 |
<center><h3><a name="tool">Cmdline tool: csvSelect</a></h3></center> |
| 170 |
<p> |
| 171 |
The package distribution contains a command-line tool called |
| 172 |
<em>csvSelect</em>. It is a fuller and more useful version of the demo |
| 173 |
program used to illustrate performance above. <em>csvSelect</em> |
| 174 |
chooses and re-arranges the columns of a CSV file as specified by its |
| 175 |
command-line arguments. Columns can be chosen by number (counting from |
| 176 |
1) or by name (as in the header row of the input). Columns appear in |
| 177 |
the output in the same order as the arguments. A different delimiter |
| 178 |
than comma can be specified. If input or output files are not |
| 179 |
specified, then stdin/stdout are used. |
| 180 |
|
| 181 |
<pre> |
| 182 |
Usage: csvSelect [OPTION...] (num|fieldname)... |
| 183 |
select numbered/named columns from a CSV file |
| 184 |
-v, -V --version show version number |
| 185 |
-o FILE --output=FILE output FILE |
| 186 |
-i FILE --input=FILE input FILE |
| 187 |
-u --unchecked ignore CSV format errors |
| 188 |
-d @ --delimiter=@ delimiter char is @ |
| 189 |
</pre> |
| 190 |
|
| 191 |
|
| 192 |
<hr> |
| 193 |
<center><h3><a name="download">Downloads</a></h3></center> |
| 194 |
<p> |
| 195 |
<b>Development version:</b><br> |
| 196 |
<br><tt><a href="http://darcs.net/">darcs</a> get |
| 197 |
http://code.haskell.org/lazy-csv</tt> |
| 198 |
|
| 199 |
<p> |
| 200 |
<b>Current released version:</b><br> |
| 201 |
lazy-csv-0.5, release date 2013.05.24 - |
| 202 |
<a href="http://hackage.haskell.org/package/lazy-csv">on Hackage</a> |
| 203 |
<br> |
| 204 |
|
| 205 |
<ul> |
| 206 |
<li> Or just <tt>cabal install lazy-csv</tt> |
| 207 |
</ul> |
| 208 |
|
| 209 |
<p> |
| 210 |
<b>Older versions:</b><br> |
| 211 |
lazy-csv-0.5, release date 2013.05.24 - Fifth release, public.<br> |
| 212 |
lazy-csv-0.4, release date 2013.02.25 - Fourth release, first public.<br> |
| 213 |
lazy-csv-0.3, release date 2011.12.12 - Third (non-public) release.<br> |
| 214 |
lazy-csv-0.2, release date 2011.10.11 - Second (non-public) release.<br> |
| 215 |
lazy-csv-0.1, release date 2009.11.20 - First (non-public) release.<br> |
| 216 |
|
| 217 |
|
| 218 |
<hr> |
| 219 |
<center><h3><a name="news">Recent news</a></h3></center> |
| 220 |
<p> |
| 221 |
Version 0.5 fixes a bug when handling (rare) CR-only line-endings. |
| 222 |
<p> |
| 223 |
Version 0.4 is the first public release. |
| 224 |
<p> |
| 225 |
Version 0.3 adds duplicate-header detection and repair. |
| 226 |
<p> |
| 227 |
Version 0.2 adds repairing of blank lines and short rows. |
| 228 |
<p> |
| 229 |
Version 0.1 is the first (but non-public) release of lazy-csv. |
| 230 |
|
| 231 |
<br> |
| 232 |
<a href="changelog.html">Complete Changelog</a><br> |
| 233 |
|
| 234 |
<hr> |
| 235 |
<center><h3><a name="who">Contacts</a></h3></center> |
| 236 |
<p> |
| 237 |
<ul> |
| 238 |
<li> <a href="mailto:[email protected]"> |
| 239 |
[email protected]</a> |
| 240 |
</ul> |
| 241 |
|
| 242 |
<p> |
| 243 |
|
| 244 |
<p><b>Licence:</b> The library is Free and Open Source Software, |
| 245 |
i.e., copyright to us, but freely licensed |
| 246 |
for your use, modification, and re-distribution. |
| 247 |
The lazy-csv library is distributed |
| 248 |
under a BSD-like 3-clause Licence - see file |
| 249 |
<a href="LICENCE-BSD3">LICENCE-BSD3</a> for more details. |
| 250 |
|
| 251 |
<hr> |
| 252 |
<p> |
| 253 |
<center><h3><a name="related">Related work</a></h3></center> |
| 254 |
<ul> |
| 255 |
<li> |
| 256 |
<a href="http://hackage.haskell.org/packages/csv"> |
| 257 |
A CSV library</a> |
| 258 |
|
| 259 |
<li> |
| 260 |
<a href="http://hackage.haskell.org/packages/bytestring-csv"> |
| 261 |
A Bytestring CSV library</a> |
| 262 |
|
| 263 |
<li> |
| 264 |
<a href="http://hackage.haskell.org/packages/spreadsheet"> |
| 265 |
A SpreadSheet CSV library</a> |
| 266 |
|
| 267 |
</ul> |
| 268 |
|
| 269 |
<hr> |
| 270 |
|
| 271 |
</body> |
| 272 |
</html> |