1csv(n) CSV processing csv(n)
2
3
4
5______________________________________________________________________________
6
8 csv - Procedures to handle CSV data.
9
11 package require Tcl 8.4
12
13 package require csv ?0.8.1?
14
15 ::csv::iscomplete data
16
17 ::csv::join values ?sepChar? ?delChar? ?delMode?
18
19 ::csv::joinlist values ?sepChar? ?delChar? ?delMode?
20
21 ::csv::joinmatrix matrix ?sepChar? ?delChar? ?delMode?
22
23 ::csv::read2matrix ?-alternate? chan m {sepChar ,} {expand none}
24
25 ::csv::read2queue ?-alternate? chan q {sepChar ,}
26
27 ::csv::report cmd matrix ?chan?
28
29 ::csv::split ?-alternate? line ?sepChar? ?delChar?
30
31 ::csv::split2matrix ?-alternate? m line {sepChar ,} {expand none}
32
33 ::csv::split2queue ?-alternate? q line {sepChar ,}
34
35 ::csv::writematrix m chan ?sepChar? ?delChar?
36
37 ::csv::writequeue q chan ?sepChar? ?delChar?
38
39______________________________________________________________________________
40
42 The csv package provides commands to manipulate information in CSV FOR‐
43 MAT (CSV = Comma Separated Values).
44
46 The following commands are available:
47
48 ::csv::iscomplete data
49 A predicate checking if the argument data is a complete csv
50 record. The result is a boolean flag indicating the completeness
51 of the data. The result is true if the data is complete.
52
53 ::csv::join values ?sepChar? ?delChar? ?delMode?
54 Takes a list of values and returns a string in CSV format con‐
55 taining these values. The separator character can be defined by
56 the caller, but this is optional. The default is ",". The quot‐
57 ing aka delimiting character can be defined by the caller, but
58 this is optional. The default is '"'. By default the quoting
59 mode delMode is "auto", surrounding values with delChar only
60 when needed. When set to "always" however, values are always
61 surrounded by the delChar instead.
62
63 ::csv::joinlist values ?sepChar? ?delChar? ?delMode?
64 Takes a list of lists of values and returns a string in CSV for‐
65 mat containing these values. The separator character can be de‐
66 fined by the caller, but this is optional. The default is ",".
67 The quoting character can be defined by the caller, but this is
68 optional. The default is '"'. By default the quoting mode
69 delMode is "auto", surrounding values with delChar only when
70 needed. When set to "always" however, values are always sur‐
71 rounded by the delChar instead. Each element of the outer list
72 is considered a record, these are separated by newlines in the
73 result. The elements of each record are formatted as usual (via
74 ::csv::join).
75
76 ::csv::joinmatrix matrix ?sepChar? ?delChar? ?delMode?
77 Takes a matrix object following the API specified for the
78 struct::matrix package and returns a string in CSV format con‐
79 taining these values. The separator character can be defined by
80 the caller, but this is optional. The default is ",". The quot‐
81 ing character can be defined by the caller, but this is op‐
82 tional. The default is ´"'. By default the quoting mode delMode
83 is "auto", surrounding values with delChar only when needed.
84 When set to "always" however, values are always surrounded by
85 the delChar instead. Each row of the matrix is considered a
86 record, these are separated by newlines in the result. The ele‐
87 ments of each record are formatted as usual (via ::csv::join).
88
89 ::csv::read2matrix ?-alternate? chan m {sepChar ,} {expand none}
90 A wrapper around ::csv::split2matrix (see below) reading CSV-
91 formatted lines from the specified channel (until EOF) and
92 adding them to the given matrix. For an explanation of the ex‐
93 pand argument see ::csv::split2matrix.
94
95 ::csv::read2queue ?-alternate? chan q {sepChar ,}
96 A wrapper around ::csv::split2queue (see below) reading CSV-for‐
97 matted lines from the specified channel (until EOF) and adding
98 them to the given queue.
99
100 ::csv::report cmd matrix ?chan?
101 A report command which can be used by the matrix methods format
102 2string and format 2chan. For the latter this command delegates
103 the work to ::csv::writematrix. cmd is expected to be either
104 printmatrix or printmatrix2channel. The channel argument, chan,
105 has to be present for the latter and must not be present for the
106 first.
107
108 ::csv::split ?-alternate? line ?sepChar? ?delChar?
109 converts a line in CSV format into a list of the values con‐
110 tained in the line. The character used to separate the values
111 from each other can be defined by the caller, via sepChar, but
112 this is optional. The default is ",". The quoting character can
113 be defined by the caller, but this is optional. The default is
114 '"'.
115
116 If the option -alternate is specified a slightly different syn‐
117 tax is used to parse the input. This syntax is explained below,
118 in the section FORMAT.
119
120 ::csv::split2matrix ?-alternate? m line {sepChar ,} {expand none}
121 The same as ::csv::split, but appends the resulting list as a
122 new row to the matrix m, using the method add row. The expansion
123 mode specified via expand determines how the command handles a
124 matrix with less columns than contained in line. The allowed
125 modes are:
126
127 none This is the default mode. In this mode it is the respon‐
128 sibility of the caller to ensure that the matrix has
129 enough columns to contain the full line. If there are not
130 enough columns the list of values is silently truncated
131 at the end to fit.
132
133 empty In this mode the command expands an empty matrix to hold
134 all columns of the specified line, but goes no further.
135 The overall effect is that the first of a series of lines
136 determines the number of columns in the matrix and all
137 following lines are truncated to that size, as if mode
138 none was set.
139
140 auto In this mode the command expands the matrix as needed to
141 hold all columns contained in line. The overall effect is
142 that after adding a series of lines the matrix will have
143 enough columns to hold all columns of the longest line
144 encountered so far.
145
146 ::csv::split2queue ?-alternate? q line {sepChar ,}
147 The same as ::csv::split, but appending the resulting list as a
148 single item to the queue q, using the method put.
149
150 ::csv::writematrix m chan ?sepChar? ?delChar?
151 A wrapper around ::csv::join taking all rows in the matrix m and
152 writing them CSV formatted into the channel chan.
153
154 ::csv::writequeue q chan ?sepChar? ?delChar?
155 A wrapper around ::csv::join taking all items in the queue q
156 (assumes that they are lists) and writing them CSV formatted
157 into the channel chan.
158
160 The format of regular CSV files is specified as
161
162 [1] Each record of a csv file (comma-separated values, as exported
163 e.g. by Excel) is a set of ASCII values separated by ",". For
164 other languages it may be ";" however, although this is not im‐
165 portant for this case as the functions provided here allow any
166 separator character.
167
168 [2] If and only if a value contains itself the separator ",", then
169 it (the value) has to be put between "". If the value does not
170 contain the separator character then quoting is optional.
171
172 [3] If a value contains the character ", that character is repre‐
173 sented by "".
174
175 [4] The output string "" represents the value ". In other words, it
176 is assumed that it was created through rule 3, and only this
177 rule, i.e. that the value was not quoted.
178
179 An alternate format definition mainly used by MS products specifies
180 that the output string "" is a representation of the empty string. In
181 other words, it is assumed that the output was generated out of the
182 empty string by quoting it (i.e. rule 2), and not through rule 3. This
183 is the only difference between the regular and the alternate format.
184
185 The alternate format is activated through specification of the option
186 -alternate to the various split commands.
187
189 Using the regular format the record
190
191 123,"123,521.2","Mary says ""Hello, I am Mary""",""
192
193
194 is parsed into the items
195
196 a) 123
197 b) 123,521.2
198 c) Mary says "Hello, I am Mary"
199 d) "
200
201
202 Using the alternate format the result is
203
204 a) 123
205 b) 123,521.2
206 c) Mary says "Hello, I am Mary"
207 d) (the empty string)
208
209 instead. As can be seen only item (d) is different, now the empty
210 string instead of a ".
211
213 This document, and the package it describes, will undoubtedly contain
214 bugs and other problems. Please report such in the category csv of the
215 Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also
216 report any ideas for enhancements you may have for either package
217 and/or documentation.
218
219 When proposing code changes, please provide unified diffs, i.e the out‐
220 put of diff -u.
221
222 Note further that attachments are strongly preferred over inlined
223 patches. Attachments can be made by going to the Edit form of the
224 ticket immediately after its creation, and then using the left-most
225 button in the secondary navigation bar.
226
228 matrix, queue
229
231 csv, matrix, package, queue, tcllib
232
234 Text processing
235
237 Copyright (c) 2002-2015 Andreas Kupries <andreas_kupries@users.sourceforge.net>
238
239
240
241
242tcllib 0.8.1 csv(n)