This module implements a simple high performance CSV (comma separated value) 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)
For CSV files with a header row, the header can be read and then used as a reference for item access with rowEntry:
import parsecsv
import os
# Prepare a file
let content = """One,Two,Three,Four
1,2,3,4
10,20,30,40
100,200,300,400
"""
writeFile("temp.csv", content)
var p: CsvParser
p.open("temp.csv")
p.readHeaderRow()
while p.readRow():
echo "new row: "
for col in items(p.headers):
echo "##", col, ":", p.rowEntry(col), "##"
p.close() CsvRow = seq[string]
CsvParser = object of BaseLexer
row*: CsvRow ## the current row
filename: string
sep, quote, esc: char
skipWhite: bool
currRow: int
headers*: seq[string] ## The columns that are defined in the csv file
## (read using `readHeaderRow <#readHeaderRow.CsvParser>`_).
## Used with `rowEntry <#rowEntry.CsvParser.string>`_).CsvError = object of IOError
proc open(my: var CsvParser; input: Stream; filename: string; separator = ',';
quote = '\"'; escape = '\x00'; skipInitialSpace = false) {...}{.raises: [Exception],
tags: [ReadIOEffect].}proc open(my: var CsvParser; filename: string; separator = ','; quote = '\"';
escape = '\x00'; skipInitialSpace = false) {...}{.raises: [CsvError, Exception],
tags: [ReadIOEffect].}proc processedRows(my: var CsvParser): int {...}{.raises: [], tags: [].}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.
Blank lines are skipped.
proc close(my: var CsvParser) {...}{.inline, raises: [Exception], tags: [].}proc readHeaderRow(my: var CsvParser) {...}{.raises: [CsvError, Exception],
tags: [ReadIOEffect].}proc rowEntry(my: var CsvParser; entry: string): var string {...}{.raises: [], tags: [].}Acceses a specified entry from the current row.
Assumes that readHeaderRow has already been called.
© 2006–2018 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/parsecsv.html