csvtools

Manage CSV files


Keywords
CSV, comma separated values, TSV
License
Apache-2.0
Install
nimble install csvtools

Documentation

CSVtools

nimble

Manage CSV files easily in Nim.

The aim is to be able to transform CSV files into typed iterators, infering layout of things like dates and number where possible, with minimal user intervention.

In this preliminary version, english locale is assumed for numbers and layout of dates must be specified. Still, the automatic typed deserialization is already quite handy.

A symmetric API exists for writing typed sequences into CSV files.

The conversion from an object of type T into a sequence of strings - or viceversa the transformation from a sequence of strings into a T - is handled by a macro. This macro assumes that T is a flat type, meaning that its members are either numbers, dates (TimeInfo) or strings.

Api documentation

Examples

Simple iterator over rows as seq[string]

import csvtools

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

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

Writing back into a file

import csvtools

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

let payments: seq[Payment] = # ...

payments.writeToCsv("payments.csv")

Writing back, one line at a time

import csvtools

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

let payments: seq[Payment] = # ...

var f = open("payments.csv", fmWrite)
# lines is an iterator of strings
for line in lines(payments):
  f.write(line)
f.close()