https://github.com/andreaferretti/csvtools
Manage CSV files in Nim
https://github.com/andreaferretti/csvtools
Last synced: 6 months ago
JSON representation
Manage CSV files in Nim
- Host: GitHub
- URL: https://github.com/andreaferretti/csvtools
- Owner: andreaferretti
- License: apache-2.0
- Created: 2015-08-13T08:59:49.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2021-01-21T09:23:22.000Z (over 4 years ago)
- Last Synced: 2025-03-24T17:53:11.875Z (7 months ago)
- Language: Nim
- Homepage: http://andreaferretti.github.io/csvtools/
- Size: 82 KB
- Stars: 48
- Watchers: 19
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
CSVtools
========[](https://github.com/yglukhov/nimble-tag)
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.The library is updated on Nim devel. For Nim up to 0.13, use version 0.1.0
of csvtools.[Api documentation](http://andreaferretti.github.io/csvtools/)
Examples
--------Simple iterator over rows as `seq[string]`
```nim
import csvtoolsfor row in csvRows("myfile.csv"):
echo row[4]
```Typed iterator supporting numbers, strings and dates
```nim
import csvtools, timestype Payment = object
time: TimeInfo
accountFrom, accountTo: string
amount: floatfor 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
```nim
import csvtoolstype Payment = object
accountFrom, accountTo: string
amount: floatlet payments: seq[Payment] = # ...
payments.writeToCsv("payments.csv")
```Writing back, one line at a time
```nim
import csvtoolstype Payment = object
accountFrom, accountTo: string
amount: floatlet 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()
```