-
Notifications
You must be signed in to change notification settings - Fork 2
/
README
161 lines (114 loc) · 3.5 KB
/
README
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
csv.erl
=======
csv.erl is the erlang library for csv records manipulating (rfc4180).
Build
==========
make && make test
API
==========
make/2
Make a CSV text record from supplied fields.
put_rec/3
Put a CSV record into a Fio.
put_recs/3
Put all CSV records into a Fio.
put_frecs/3
Put all CSV records into a file. Open a file with [append] modes.
put_frecs/4
Put all CSV records into a file. Open a file with supplied modes.
parse/2
Parse a CSV text record into fields.
get_rec/2
Get a CSV record from a Fio.
get_recs/2
Get all CSV records from a Fio.
get_frecs/2
Get all CSV records from a file.
See details in doc/index.html (created by "make doc" command).
All functions get a csv record as the first argument. It contain the next fields:
sep
a field separator
"," - by default
quote
a quote construct
"\"" - by default
eor
an end of record construct
"\n" - by default
cb_make
a callback for make/2.
undefined - by default
cb_parse
a callback for parse/2 (used by get_rec/2, get_recs/2, get_frecs/2).
undefined - by default
Callbacks
==========
A callback take two arguments:
Opts
csv record (see csv.hrl).
Rec
a record.
And must return:
{ok, Rec_new}
a converted record.
{error, Reason}
an error.
make/2 callback is called before the main make/2 code and must convert a user
supplied data to a list of fields with which make/2 works.
For a make/2 callback, Rec is a user supplied record from a second make/2
argument. Rec_new for a make/2 callback is a list of fields to make a CSV
record from.
parse/2 callback is called after the main parse/2 code and must convert a list
of fields to a user data format.
For a parse/2 callback, Rec is a list of fields we parsed from a user supplied
text from a second parse/2 argument (a CSV text record). Rec_new for a parse/2
callback is a parsed record in a user format.
Examples
==========
The example csv file used in examples:
one,11,info1
two,22,info2
Make a csv record from fields:
1> csv:make(#csv{}, [1,2,3]).
{ok, "1,2,3\n"}
Make a csv record from fields with ; as a separator:
1> csv:make(#csv{sep = ";"}, [1,2,3]).
{ok, "1;2;3\n"}
Make a csv record from a tuple:
1> F = fun(_Opts, Rec) -> {ok, tuple_to_list(Rec)} end.
#Fun<erl_eval.12.80484245>
2> csv:make(#csv{cb_make=F}, {1,2,3}).
{ok,"1,2,3\n"}
Append csv records into a file:
1> csv:put_frecs(#csv{}, [[1, 2, "field"], [yet, "another", row]], "testfile").
ok
Read all records from a file:
1> csv:get_frecs(#csv{}, "ex.csv").
{ok,[["one","11","info1"],["two","22","info2"]]}
Read all records as tuples from a file:
1> F = fun(_Opts, Rec) -> {ok, list_to_tuple(Rec)} end.
#Fun<erl_eval.12.80484245>
2> csv:get_frecs(#csv{cb_parse=F}, "ex.csv").
{ok,[{"one","11","info1"},{"two","22","info2"}]}
Read all records as records from a file:
13> F = fun(_Opts, Rec) -> csv:cb_list_to_rec(Rec, my_rec) end.
#Fun<erl_eval.12.80484245>
14> csv:get_frecs(#csv{cb_parse=F}, "ex.csv").
{ok,[{my_rec, "one","11","info1"},
{my_rec, "two","22","info2"}]}
Write records to a file(convert each field from unicode to utf8):
25> Ff = fun(Field) when is_list(Field) ->
25> Bin = unicode:characters_to_binary(Field),
25> binary_to_list(Bin);
25> (Field) ->
25> Field
25> end.
#Fun<erl_eval.6.50752066>
26> Fr = fun(_Opts, Rec) ->
26> {ok, lists:map(Ff, Rec)}
26> end.
#Fun<erl_eval.12.50752066>
27> csv:put_frecs(#csv{cb_make=Fr}, [["Тест", 1233]], "test.csv").
ok
28> csv:put_frecs(#csv{cb_make=Fr}, [["我", 123]], "test.csv").
ok