Module csvtools

Manage CSV files easily in Nim. At this moment only reading is supported.

Example of a simple iterator over rows as seq[string]

import csvtools

for row in csvRows("myfile.csv"):
  echo row[4]

Example of a typed iterator supporting numbers, strings and dates:

import csvtools, times

type Payment = object
  time: TimeInfo
  accountFrom, accountTo: string
  amount: float

for payment in csv[Payment]("payments.csv",
  dateLayout = "yyyy-MM-dd HH:mm:ss", skipHeader = true):
  echo payment.amount
  echo payment.time.weekday

Procs

proc string2int(s: string): int {.raises: [OverflowError, AssertionError], tags: [].}

Parses a string as integer, throwing an assertion error over failure.

This is a helper proc that should not be needed explicitly in client code.

  Source
proc string2float(s: string): float {.raises: [AssertionError], tags: [].}

Parses a string as float, throwing an assertion error over failure.

This is a helper proc that should not be needed explicitly in client code.

  Source
proc genPack[](T: typedesc; dateLayout: string = nil): proc (s: seq[string]): T:type

Generater a deserializer for the type T.

This is a procedure that will convert from a sequence of strings to an object of type T.

The type T must be a flat object whose fields are numbers, strings or TimeInfo.

  Source
proc genUnpack[](T: typedesc; dateLayout: string = nil): proc (t: T:type): seq[string]

Generater a serializer for the type T.

This is a procedure that will convert from an object of type T to a sequence of strings.

The type T must be a flat object whose fields are numbers, strings or TimeInfo.

  Source
proc quoteString(s: string; quote = '\"'; escape = '\"'): string {.inline, raises: [],
    tags: [].}
Quote a single field in order to write it in CSV format   Source
proc connect(s: seq[string]; separator = ','; quote = '\"'; escape = '\"';
            quoteAlways = false): string {.raises: [], tags: [].}

Returns a string that represents a row in a CSV, obtained by quoting and joining the fields in s.

The writer'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.
  • escape: removes any special meaning from the following character;
  • quoteAlways: If true, fields are quoted regardless of whether they contain special characters.
  Source
proc line[T](t: T; separator = ','; quote = '\"'; escape = '\"'; quoteAlways = false): string

Returns a string that represents a row in a CSV, obtained by quoting and joining the fields in t.

The type T must be a flat object whose fields are numbers, strings or TimeInfo.

The writer'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.
  • escape: removes any special meaning from the following character;
  • quoteAlways: If true, fields are quoted regardless of whether they contain special characters.
  Source
proc writeToCsv[T](ts: openArray[T]; f: var File; separator = ','; quote = '\"';
                  escape = '\"'; quoteAlways = false)

Writes rows in a CSV file f, each one obtained by calling line on an element in ts.

The type T must be a flat object whose fields are numbers, strings or TimeInfo.

The writer'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.
  • escape: removes any special meaning from the following character;
  • quoteAlways: If true, fields are quoted regardless of whether they contain special characters.
  Source
proc writeToCsv[T](ts: openArray[T]; path: string; separator = ','; quote = '\"';
                  escape = '\"'; quoteAlways = false)

Writes rows in a CSV file with path path, each one obtained by calling line on an element in ts.

The type T must be a flat object whose fields are numbers, strings or TimeInfo.

The writer'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.
  • escape: removes any special meaning from the following character;
  • quoteAlways: If true, fields are quoted regardless of whether they contain special characters.
  Source

Iterators

iterator csvRows(path: string; separator = ','; quote = '\"'; escape = '\0';
                skipInitialSpace = false): CsvRow {.raises: [Exception, CsvError],
    tags: [ReadIOEffect].}

Raw iterator over the rows of path.

Does not perform any deserialization, so elements of the iterators are plain seq[string].

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
iterator csv[T](path: string; separator = ','; quote = '\"'; escape = '\0';
               skipInitialSpace = false; skipHeader = false; dateLayout: string = nil): T

Typed iterator over the rows of path.

The type T must be a flat object whose fields are numbers, strings or TimeInfo.

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.
  • skipHeader: If true, the first line of the file will be considered a header, and thus skipped
  Source
iterator lines[T](ts: openArray[T]; separator = ','; quote = '\"'; escape = '\"';
                 quoteAlways = false): string

Iterator over the rows in a CSV, each one obtained by calling line on an element in ts.

The type T must be a flat object whose fields are numbers, strings or TimeInfo.

The writer'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.
  • escape: removes any special meaning from the following character;
  • quoteAlways: If true, fields are quoted regardless of whether they contain special characters.
  Source
iterator lines[T](ts: iterator (): T; separator = ','; quote = '\"'; escape = '\"';
                 quoteAlways = false): string

Iterator over the rows in a CSV, each one obtained by calling line on an element in ts.

The type T must be a flat object whose fields are numbers, strings or TimeInfo.

The writer'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.
  • escape: removes any special meaning from the following character;
  • quoteAlways: If true, fields are quoted regardless of whether they contain special characters.
  Source