Hi,
I’m receiving the above error from the below function, and I’ve no idea why (I expected channels to be safely handled via the with_file
function).
The code uses the opam csv package and core.
let read3 filename =
let csv = In_channel.with_file
filename
~f:( fun channel ->
Csv.of_channel
~separator:','
~has_header:true
~excel_tricks:false
channel
)
in
let headers = Csv.Rows.header csv in
print_endline ( "read headers : " ^ ( String.concat ~sep:"," headers ) );
Csv.Rows.fold_left
~init:[]
~f:(fun acc row ->
print_endline ( "parsing row : " ^ ( String.concat ~sep:"," ( Csv.Row.to_list row ) ) );
let newrecord =
List.fold
headers
~init:empty
~f:(fun record header ->
let value = Csv.Row.find row header in
set_field record header value
)
in
print_endline "parsing row completed";
newrecord :: acc
)
csv
This is the test csv file:
"heading1","heading2","heading3"
"A","B","C"
"1","2","3"
and this is the output generated:
read headers : heading1,heading2,heading3
parsing row : A,B,C
parsing row completed
parsing row : 1,2,3
parsing row completed
Uncaught exception:
(Sys_error "Bad file descriptor")
Any insight greatly appreciated!
Thanks.