Module parsecsv

This module implements a simple high performance CSV (comma separated value) parser.

Example: How to use the parser

import os, parsecsv, streams
var s = newFileStream(paramStr(1), fmRead)
if s == nil: quit("cannot open the file" & paramStr(1))
var x: CsvParser
open(x, s, paramStr(1))
while readRow(x):
  echo "new row: "
  for val in items(x.row):
    echo "##", val, "##"
close(x)

Types

CsvRow = seq[string]
a row in a CSV file   Source
CsvParser = object of BaseLexer
  row*: CsvRow                ## the current row
  filename: string
  sep, quote, esc: char
  skipWhite: bool
  currRow: int
the parser object.   Source
CsvError = object of IOError
exception that is raised if a parsing error occurs   Source

Procs

proc open(my: var CsvParser; input: Stream; filename: string; separator = ','; 
          quote = '\"'; escape = '\0'; skipInitialSpace = false) {.
    raises: [Exception], tags: [ReadIOEffect].}
initializes the parser with an input stream. Filename is only used for nice error messages. The parser's behaviour can be controlled by the diverse optional parameters:
  • separator: character used to separate fields
  • quote: Used to quote fields containing special characters like separator, quote or new-line characters. '0' disables the parsing of quotes.
  • escape: removes any special meaning from the following character; '0' disables escaping; if escaping is disabled and quote is not '0', two quote characters are parsed one literal quote character.
  • skipInitialSpace: If true, whitespace immediately following the separator is ignored.
  Source
proc processedRows(my: var CsvParser): int {.raises: [], tags: [].}
returns number of the processed rows   Source
proc readRow(my: var CsvParser; columns = 0): bool {.
    raises: [CsvError, Exception], tags: [ReadIOEffect].}
reads the next row; if columns > 0, it expects the row to have exactly this many columns. Returns false if the end of the file has been encountered else true.   Source
proc close(my: var CsvParser) {.inline, raises: [Exception], tags: [].}
closes the parser my and its associated input stream.   Source