Module Delimited_kernel.Read

exception Bad_csv_formatting of string list * string

Row up to the error, and the field with the error up to the point of failure. Same as Expert.Parse_state.Bad_csv_formatting.

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.Read.t =
    let open Delimited_kernel.Read.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.Read.list_of_string
      ~header:`Yes
      parse
      "foo,bar\n2,\"hello, world\"\n"
  ;;
include Core.Applicative.S with type 'a t := 'a t
val return : 'a 'p 'q. 'a -> 'a t

Convert a value to a t.

val apply : 'a 'b 'p 'q. ('a -> 'b) t -> 'a t -> 'b t

Applies the functions in one t to the values in another. Well-behaved applicatives satisfy these "laws", using <*> as infix apply:

  • return Fn.id <*> t is equivalent to t
  • return Fn.compose <*> tf <*> tg <*> tx is equivalent to tf <*> (tg <*> tx)
  • return f <*> return x is equivalent to return (f x)
  • tf <*> return x is equivalent to return (fun f -> f x) <*> tf
val both : 'a 'b 'p 'q. 'a t -> 'b t -> ('a * 'b) t

Combines values in two ts as tuples. Using <*> as infix apply, equivalent to return (fun a b -> a, b) <*> ta <*> tb.

val map : 'a 'b 'p 'q. 'a t -> f:('a -> 'b) -> 'b t

Transforms the contents of a t. Using <*> as infix apply, equivalent to return f <*> t.

val map2 : 'a 'b 'c 'p 'q. 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t

Combines the contents of two ts. Using <*> as infix apply, equivalent to return f <*> ta <*> tb.

val map3 : 'a 'b 'c 'd 'p 'q. 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd t

Combines the contents of three ts. Using <*> as infix apply, equivalent to return f <*> ta <*> tb <*> tc.

val all : 'a 'p 'q. 'a t list -> 'a list t

Combines a list of t.

val all_unit : 'p 'q. unit t list -> unit t

Combines a list of t whose contents are unimportant.

val (<*>) : 'a 'b 'p 'q. ('a -> 'b) t -> 'a t -> 'b t
val (<*) : 'a 'p 'q. 'a t -> unit t -> 'a t
val (*>) : 'a 'p 'q. unit t -> 'a t -> 'a t
val (>>|) : 'a 'b 'p 'q. 'a t -> ('a -> 'b) -> 'b t
module Applicative_infix : sig ... end
module Open_on_rhs_intf : sig ... end
include Core.Applicative.Let_syntax with type 'a t := 'a t with module Open_on_rhs_intf := Open_on_rhs_intf
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.

val label : 'a t -> Core.Info.t -> 'a t

Provide a label to be used in exceptions raised by this builder

module Record_builder : Record_builder.S with type 'a applicative = 'a t
module Fields_O : sig ... end

The following are convenience functions that build on Record_builder.field to make it easy to define a t Delimited.Read.t for some record type t.

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

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

Read CSV file.

val fold_lines : ?buffer_size:int -> ?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) -> Core.In_channel.t -> 'b

Read CSV file, processing line by line.

module Streaming : sig ... end
module Expert : sig ... end

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