package core_extended

  1. Overview
  2. Docs

Read CSVs & CSV-like delimited formats (following the CSV quoting behaviour).

These formats are loosely documented by RFC 4180: https://www.ietf.org/rfc/rfc4180.txt

exception Bad_csv_formatting of string list * string
type 'a t

This provides an applicative interface for constructing values from a csv file.

An 'a t describes how to build an OCaml model 'a for each row.

Simple example:

type t =
  { foo : int
  ; bar : string
  }

(* Describes how to generate a [t] from a row of a csv file *)
let parse : t Delimited_kernel.Parse.t =
  let open Delimited_kernel.Parse.Let_syntax in
  let%map_open foo = at_header "foo" ~f:Int.of_string
  and bar = at_header "bar" ~f:String.of_string in
  { foo; bar }
;;

let _ =
  Delimited_kernel.Parse.list_of_string ~header:`Yes parse
    "foo,bar\n2,\"hello, world\"\n"
;;
include Core_kernel.Applicative.S with type 'a t := 'a t
val return : 'a -> 'a t
val map : 'a t -> f:('a -> 'b) -> 'b t
val both : 'a t -> 'b t -> ('a * 'b) t
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t

same as apply

val (<*) : 'a t -> unit t -> 'a t
val (*>) : unit t -> 'a t -> 'a t
val (>>|) : 'a t -> ('a -> 'b) -> 'b t
val apply : ('a -> 'b) t -> 'a t -> 'b t
val map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t
val map3 : 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd t
val all : 'a t list -> 'a list t
val all_unit : unit t list -> unit t
val all_ignore : unit t list -> unit t
  • deprecated [since 2018-02] Use [all_unit]
module Applicative_infix : sig ... end
module Let_syntax : sig ... end
val at_index : int -> f:(string -> 'a) -> 'a t

Read a field at the given index. Use f to convert the field from string.

val at_header : string -> f:(string -> 'a) -> 'a t

Read a field at the given header. Use f to convert the field from string.

Note that if the given header is not provided through either the file or the ~header argument to the parsers, this will fail at runtime.

val at_header_opt : string -> f:(string option -> 'a) -> 'a t

Read a field at the given header, if it exists. Use f to convert the field from string.

module On_invalid_row : sig ... end
module Header : sig ... end

Header parsing control

module Row : sig ... end

Whole-row parsing.

val fold_string : ?strip:bool -> ?sep:char -> ?quote:[ `No_quoting | `Using of char ] -> ?header:Header.t -> ?on_invalid_row:'a On_invalid_row.t -> 'a t -> init:'b -> f:('b -> 'a -> 'b) -> string -> 'b

Fold the CSV rows contained in the given string.

val list_of_string : ?strip:bool -> ?sep:char -> ?quote:[ `No_quoting | `Using of char ] -> ?header:Header.t -> ?on_invalid_row:'a On_invalid_row.t -> 'a t -> string -> 'a list

Load the CSV as a list

module Expert : sig ... end

Experts only. If you really think you need a function in this module, please talk to a delimited dev first.

OCaml

Innovation. Community. Security.