csvt#
Declarative CSV codecs with bytesrw streaming.
Overview#
Csvt provides bidirectional CSV encoding and decoding using a combinator-based approach inspired by Jsont. Define a typed row codec once and use it for both decoding CSV files into OCaml records and encoding records back to CSV. Column names are resolved at decode time for O(1) per-row field access.
Streaming I/O is built on bytesrw:
decode reads from a Bytes.Reader.t and encode writes to a
Bytes.Writer.t, so CSV processing composes with other bytesrw-based codecs
in a pipeline. For simple cases, decode_file, decode_string, and
fold_file provide direct access.
Installation#
opam install csvt
Usage#
type point = { x : float; y : float; label : string }
let point_codec =
Csvt.(Row.(
obj (fun x y label -> { x; y; label })
|> col "x" float ~enc:(fun p -> p.x)
|> col "y" float ~enc:(fun p -> p.y)
|> col "label" string ~enc:(fun p -> p.label)
|> finish
))
(* Decode from a file *)
let () =
match Csvt.decode_file point_codec "points.csv" with
| Ok points -> List.iter (fun p -> Printf.printf "%s\n" p.label) points
| Error e -> prerr_endline (Csvt.error_to_string e)
(* Streaming fold without building a list *)
let sum =
Csvt.fold_file point_codec "points.csv"
(fun acc p -> acc +. p.x) 0.0
(* Encode to a bytesrw writer *)
let () =
let buf = Buffer.create 256 in
let w = Bytesrw.Bytes.Writer.of_buffer buf in
Csvt.encode point_codec points w
API Overview#
- Field codecs --
string,int,float,bool,option,nullable_float,nullable_int col_map-- Custom field codec fromdec/encfunctionsRowmodule -- Row builder:obj,col,finishdecode_file,decode_channel,decode_string-- Batch decodingfold_file,fold_channel-- Streaming fold over rowsdecode-- Streaming decode from a bytesrw readerencode-- Streaming encode to a bytesrw writercol-- Single-column projection queryupdate_col,delete_col-- Column-level transformscol_names,col_count-- Codec introspection
Licence#
ISC License. See LICENSE.md for details.