1csv(n)                          CSV processing                          csv(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       csv - Procedures to handle CSV data.
9

SYNOPSIS

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

DESCRIPTION

42       The csv package provides commands to manipulate information in CSV FOR‐
43       MAT (CSV = Comma Separated Values).
44

COMMANDS

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
66              defined 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
82              optional. The default is  ´"'.   By  default  the  quoting  mode
83              delMode  is  "auto",  surrounding  values with delChar only when
84              needed. When set to "always" however,  values  are  always  sur‐
85              rounded  by the delChar instead.  Each row of the matrix is con‐
86              sidered a record, these are separated by newlines in the result.
87              The  elements  of  each  record  are  formatted  as  usual  (via
88              ::csv::join).
89
90       ::csv::read2matrix ?-alternate? chan m {sepChar ,} {expand none}
91              A wrapper around ::csv::split2matrix (see  below)  reading  CSV-
92              formatted  lines  from  the  specified  channel  (until EOF) and
93              adding them to the given  matrix.  For  an  explanation  of  the
94              expand argument see ::csv::split2matrix.
95
96       ::csv::read2queue ?-alternate? chan q {sepChar ,}
97              A wrapper around ::csv::split2queue (see below) reading CSV-for‐
98              matted lines from the specified channel (until EOF)  and  adding
99              them to the given queue.
100
101       ::csv::report cmd matrix ?chan?
102              A  report command which can be used by the matrix methods format
103              2string and format 2chan. For the latter this command  delegates
104              the  work  to  ::csv::writematrix.  cmd is expected to be either
105              printmatrix or printmatrix2channel. The channel argument,  chan,
106              has to be present for the latter and must not be present for the
107              first.
108
109       ::csv::split ?-alternate? line ?sepChar? ?delChar?
110              converts a line in CSV format into a list  of  the  values  con‐
111              tained  in  the  line. The character used to separate the values
112              from each other can be defined by the caller, via  sepChar,  but
113              this  is optional. The default is ",". The quoting character can
114              be defined by the caller, but this is optional. The  default  is
115              '"'.
116
117              If  the option -alternate is specified a slightly different syn‐
118              tax is used to parse the input. This syntax is explained  below,
119              in the section FORMAT.
120
121       ::csv::split2matrix ?-alternate? m line {sepChar ,} {expand none}
122              The  same  as  ::csv::split, but appends the resulting list as a
123              new row to the matrix m, using the method add row. The expansion
124              mode  specified  via expand determines how the command handles a
125              matrix with less columns than contained  in  line.  The  allowed
126              modes are:
127
128              none   This  is the default mode. In this mode it is the respon‐
129                     sibility of the caller to  ensure  that  the  matrix  has
130                     enough columns to contain the full line. If there are not
131                     enough columns the list of values is  silently  truncated
132                     at the end to fit.
133
134              empty  In  this mode the command expands an empty matrix to hold
135                     all columns of the specified line, but goes  no  further.
136                     The overall effect is that the first of a series of lines
137                     determines the number of columns in the  matrix  and  all
138                     following  lines  are  truncated to that size, as if mode
139                     none was set.
140
141              auto   In this mode the command expands the matrix as needed  to
142                     hold all columns contained in line. The overall effect is
143                     that after adding a series of lines the matrix will  have
144                     enough  columns  to  hold all columns of the longest line
145                     encountered so far.
146
147       ::csv::split2queue ?-alternate? q line {sepChar ,}
148              The same as ::csv::split, but appending the resulting list as  a
149              single item to the queue q, using the method put.
150
151       ::csv::writematrix m chan ?sepChar? ?delChar?
152              A wrapper around ::csv::join taking all rows in the matrix m and
153              writing them CSV formatted into the channel chan.
154
155       ::csv::writequeue q chan ?sepChar? ?delChar?
156              A wrapper around ::csv::join taking all items  in  the  queue  q
157              (assumes  that  they  are  lists) and writing them CSV formatted
158              into the channel chan.
159

FORMAT

161       The format of regular CSV files is specified as
162
163       [1]    Each record of a csv file (comma-separated values,  as  exported
164              e.g.  by  Excel)  is a set of ASCII values separated by ",". For
165              other languages it may be ";"  however,  although  this  is  not
166              important for this case as the functions provided here allow any
167              separator character.
168
169       [2]    If and only if a value contains itself the separator  ",",  then
170              it  (the  value) has to be put between "". If the value does not
171              contain the separator character then quoting is optional.
172
173       [3]    If a value contains the character ", that  character  is  repre‐
174              sented by "".
175
176       [4]    The  output string "" represents the value ". In other words, it
177              is assumed that it was created through rule  3,  and  only  this
178              rule, i.e. that the value was not quoted.
179
180       An  alternate  format  definition  mainly used by MS products specifies
181       that the output string "" is a representation of the empty  string.  In
182       other  words,  it  is  assumed that the output was generated out of the
183       empty string by quoting it (i.e. rule 2), and not through rule 3.  This
184       is the only difference between the regular and the alternate format.
185
186       The  alternate  format is activated through specification of the option
187       -alternate to the various split commands.
188

EXAMPLE

190       Using the regular format the record
191
192              123,"123,521.2","Mary says ""Hello, I am Mary""",""
193
194
195       is parsed into the items
196
197              a) 123
198              b) 123,521.2
199              c) Mary says "Hello, I am Mary"
200              d) "
201
202
203       Using the alternate format the result is
204
205              a) 123
206              b) 123,521.2
207              c) Mary says "Hello, I am Mary"
208              d) (the empty string)
209
210       instead. As can be seen only item  (d)  is  different,  now  the  empty
211       string instead of a ".
212

BUGS, IDEAS, FEEDBACK

214       This  document,  and the package it describes, will undoubtedly contain
215       bugs and other problems.  Please report such in the category csv of the
216       Tcllib  Trackers  [http://core.tcl.tk/tcllib/reportlist].   Please also
217       report any ideas for enhancements  you  may  have  for  either  package
218       and/or documentation.
219
220       When proposing code changes, please provide unified diffs, i.e the out‐
221       put of diff -u.
222
223       Note further that  attachments  are  strongly  preferred  over  inlined
224       patches.  Attachments  can  be  made  by  going to the Edit form of the
225       ticket immediately after its creation, and  then  using  the  left-most
226       button in the secondary navigation bar.
227

SEE ALSO

229       matrix, queue
230

KEYWORDS

232       csv, matrix, package, queue, tcllib
233

CATEGORY

235       Text processing
236
238       Copyright (c) 2002-2015 Andreas Kupries <andreas_kupries@users.sourceforge.net>
239
240
241
242
243tcllib                               0.8.1                              csv(n)
Impressum