File: csv.man

package info (click to toggle)
tcllib 1.21%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 69,456 kB
  • sloc: tcl: 266,493; ansic: 14,259; sh: 2,936; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 112; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (247 lines) | stat: -rw-r--r-- 8,630 bytes parent folder | download | duplicates (2)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
[comment {-*- tcl -*-}]
[vset VERSION 0.8.1]
[manpage_begin csv n [vset VERSION]]
[see_also matrix]
[see_also queue]
[keywords csv]
[keywords matrix]
[keywords package]
[keywords queue]
[keywords tcllib]
[copyright {2002-2015 Andreas Kupries <andreas_kupries@users.sourceforge.net>}]
[moddesc   {CSV processing}]
[titledesc {Procedures to handle CSV data.}]
[category  {Text processing}]
[require Tcl 8.4]
[require csv [opt [vset VERSION]]]
[description]

[para]

The [package csv] package provides commands to manipulate information
in CSV [sectref FORMAT] (CSV = Comma Separated Values).

[section COMMANDS]
[para]

The following commands are available:

[list_begin definitions]

[call [cmd ::csv::iscomplete] [arg data]]

A predicate checking if the argument [arg data] is a complete csv
record. The result is a boolean flag indicating the completeness of
the data. The result is true if the data is complete.

[call [cmd ::csv::join] [arg values] [opt [arg sepChar]] [opt [arg delChar]] [opt [arg delMode]]]

Takes a list of values and returns a string in CSV format containing
these values. The separator character can be defined by the caller,
but this is optional. The default is ",". The quoting aka delimiting character can
be defined by the caller, but this is optional. The default is '"'.

By default the quoting mode [arg delMode] is "auto", surrounding
values with [arg delChar] only when needed. When set to "always"
however, values are always surrounded by the [arg delChar] instead.

[call [cmd ::csv::joinlist] [arg values] [opt [arg sepChar]] [opt [arg delChar]] [opt [arg delMode]]]

Takes a list of lists of values and returns a string in CSV format
containing these values. The separator character can be defined by the
caller, but this is optional. The default is ",". The quoting character
can be defined by the caller, but this is optional. The default is '"'.

By default the quoting mode [arg delMode] is "auto", surrounding
values with [arg delChar] only when needed. When set to "always"
however, values are always surrounded by the [arg delChar] instead.

Each element of the outer list is considered a record, these are
separated by newlines in the result. The elements of each record are
formatted as usual (via [cmd ::csv::join]).

[call [cmd ::csv::joinmatrix] [arg matrix] [opt [arg sepChar]] [opt [arg delChar]] [opt [arg delMode]]]

Takes a [arg matrix] object following the API specified for the
struct::matrix package and returns a string in CSV format containing
these values. The separator character can be defined by the caller,
but this is optional. The default is ",". The quoting character
can be defined by the caller, but this is optional. The default is
'"'.

By default the quoting mode [arg delMode] is "auto", surrounding
values with [arg delChar] only when needed. When set to "always"
however, values are always surrounded by the [arg delChar] instead.

Each row of the matrix is considered a record, these are
separated by newlines in the result. The elements of each record are
formatted as usual (via [cmd ::csv::join]).

[call [cmd ::csv::read2matrix] [opt [option -alternate]] [arg "chan m"] "{[arg sepChar] ,} {[arg expand] none}"]

A wrapper around [cmd ::csv::split2matrix] (see below) reading
CSV-formatted lines from the specified channel (until EOF) and adding
them to the given matrix. For an explanation of the [arg expand]
argument see [cmd ::csv::split2matrix].

[call [cmd ::csv::read2queue] [opt [option -alternate]] [arg "chan q"] "{[arg sepChar] ,}"]

A wrapper around [cmd ::csv::split2queue] (see below) reading
CSV-formatted lines from the specified channel (until EOF) and adding
them to the given queue.

[call [cmd ::csv::report] [arg "cmd matrix"] [opt [arg chan]]]

A report command which can be used by the matrix methods

[cmd "format 2string"] and [cmd "format 2chan"]. For the latter this
command delegates the work to [cmd ::csv::writematrix]. [arg cmd] is
expected to be either [method printmatrix] or

[method printmatrix2channel]. The channel argument, [arg chan], has
to be present for the latter and must not be present for the first.

[call [cmd ::csv::split] [opt [option -alternate]] [arg line] [opt [arg sepChar]] [opt [arg delChar]]]

converts a [arg line] in CSV format into a list of the values
contained in the line. The character used to separate the values from
each other can be defined by the caller, via [arg sepChar], but this
is optional. The default is ",". The quoting character can be defined
by the caller, but this is optional. The default is '"'.

[para]

If the option [option -alternate] is specified a slightly different
syntax is used to parse the input. This syntax is explained below, in
the section [sectref FORMAT].

[call [cmd ::csv::split2matrix] [opt [option -alternate]] [arg "m line"] "{[arg sepChar] ,} {[arg expand] none}"]

The same as [cmd ::csv::split], but appends the resulting list as a
new row to the matrix [arg m], using the method [cmd "add row"]. The
expansion mode specified via [arg expand] determines how the command
handles a matrix with less columns than contained in [arg line]. The
allowed modes are:

[list_begin definitions]

[def [const none]]

This is the default mode. In this mode it is the responsibility of the
caller to ensure that the matrix has enough columns to contain the
full line. If there are not enough columns the list of values is
silently truncated at the end to fit.

[def [const empty]]

In this mode the command expands an empty matrix to hold all columns
of the specified line, but goes no further. The overall effect is that
the first of a series of lines determines the number of columns in the
matrix and all following lines are truncated to that size, as if mode
[const none] was set.

[def [const auto]]

In this mode the command expands the matrix as needed to hold all
columns contained in [arg line]. The overall effect is that after
adding a series of lines the matrix will have enough columns to hold
all columns of the longest line encountered so far.

[list_end]

[call [cmd ::csv::split2queue] [opt [option -alternate]] [arg "q line"] "{[arg sepChar] ,}"]

The same as [cmd ::csv::split], but appending the resulting list as a
single item to the queue [arg q], using the method [cmd put].

[call [cmd ::csv::writematrix] [arg "m chan"] [opt [arg sepChar]] [opt [arg delChar]]]

A wrapper around [cmd ::csv::join] taking all rows in the matrix
[arg m] and writing them CSV formatted into the channel [arg chan].

[call [cmd ::csv::writequeue] [arg "q chan"] [opt [arg sepChar]] [opt [arg delChar]]]

A wrapper around [cmd ::csv::join] taking all items in the queue
[arg q] (assumes that they are lists) and writing them CSV formatted
into the channel [arg chan].

[list_end]

[section FORMAT]
[para]

The format of regular CSV files is specified as

[list_begin enumerated]

[enum]
Each record of a csv file (comma-separated values, as exported e.g. by
Excel) is a set of ASCII values separated by ",". For other languages
it may be ";" however, although this is not important for this case as
the functions provided here allow any separator character.

[enum]
If and only if a value contains itself the separator ",", then it (the
value) has to be put between "". If the value does not contain the
separator character then quoting is optional.

[enum]
If a value contains the character ", that character is represented by "".

[enum]
The output string "" represents the value ". In other words, it is
assumed that it was created through rule 3, and only this rule,
i.e. that the value was not quoted.

[list_end]
[para]

An alternate format definition mainly used by MS products specifies
that the output string "" is a representation of the empty
string. In other words, it is assumed that the output was generated
out of the empty string by quoting it (i.e. rule 2), and not through
rule 3. This is the only difference between the regular and the
alternate format.

[para]

The alternate format is activated through specification of the option
[option -alternate] to the various split commands.

[section EXAMPLE]

Using the regular format the record

[para]
[example {
123,"123,521.2","Mary says ""Hello, I am Mary""",""
}]

[para]
is parsed into the items

[para]
[example {
a) 123
b) 123,521.2
c) Mary says "Hello, I am Mary"
d) "
}]
[para]

Using the alternate format the result is

[para]
[example {
a) 123
b) 123,521.2
c) Mary says "Hello, I am Mary"
d) (the empty string)
}]

instead. As can be seen only item (d) is different, now the empty string
instead of a ".

[vset CATEGORY csv]
[include ../common-text/feedback.inc]
[manpage_end]