-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSV.ml
More file actions
62 lines (56 loc) · 1.34 KB
/
CSV.ml
File metadata and controls
62 lines (56 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
exception Fichier_mal_forme
let list_of_ligne ligne =
(Str.split (Str.regexp ";") ligne)
let lire_csv f =
let entree = (open_in f)
and resultat = ref []
in begin
try
let ligne = ref (list_of_ligne (input_line entree))
in
let nb_champs = (List.length !ligne)
in
while true do
if (List.length !ligne) != nb_champs then
raise Fichier_mal_forme
else
resultat := List.append !resultat [ !ligne ];
ligne := (list_of_ligne (input_line entree))
done
with
| End_of_file -> close_in entree
end;
!resultat
let liste_string_vers_liste_entier ll =
let l = ref ll
and r = ref []
in
while !l <> [] do
try
r := List.append !r [ (int_of_string (List.hd !l)) ];
l := (List.tl !l)
with
| _ -> raise Not_found
done;
!r
let lire_csv_entiers f =
let entree = (open_in f)
and resultat = ref []
in begin
try
let ligne = ref (liste_string_vers_liste_entier (list_of_ligne (input_line entree)))
in
let nb_champs = (List.length !ligne)
in
while true do
if (List.length !ligne) != nb_champs then
raise Fichier_mal_forme
else
resultat := List.append !resultat [ !ligne ];
ligne := (liste_string_vers_liste_entier (list_of_ligne (input_line entree)))
done
with
| End_of_file -> close_in entree
end;
!resultat;;
lire_csv test.csv