csv(3tcl)

πŸ“¦
version 2.0 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

csv β€” the little procedure squad that handles all your CSV data, bestie. Comma separated values, no cap.


✨ SYNOPSIS ✨

package require Tcl 8.5 9

package require csv ?0.8.1?

::csv::iscomplete data

::csv::join values ?sepChar? ?delChar? ?delMode?

::csv::joinlist values ?sepChar? ?delChar? ?delMode?

::csv::joinmatrix matrix ?sepChar? ?delChar? ?delMode?

::csv::read2matrix ?-alternate? chan m {sepChar ,} {expand none}

::csv::read2queue ?-alternate? chan q {sepChar ,}

::csv::report cmd matrix ?chan?

::csv::split ?-alternate? line ?sepChar? ?delChar?

::csv::split2matrix ?-alternate? m line {sepChar ,} {expand none}

::csv::split2queue ?-alternate? q line {sepChar ,}

::csv::writematrix m chan ?sepChar? ?delChar?

::csv::writequeue q chan ?sepChar? ?delChar?

🌸 DESCRIPTION 🌸

Okay so the csv package is literally here to give you commands that manipulate information in CSV FORMAT (CSV = Comma Separated Values, iykyk). That's the whole vibe. She's focused, she's iconic, she does one thing and she does it πŸ’…


πŸ‘‘ COMMANDS πŸ‘‘

These are the commands you get, bestie. All of them. Every single one.

::csv::iscomplete data

A lil predicate that checks whether the argument data is a complete csv record. The result is a boolean flag telling you how complete the data is, and it's true when the data is complete. That's it, that's the girl. ✨

::csv::join values ?sepChar? ?delChar? ?delMode?

Takes a list of values and hands you back a string in CSV format containing those values. You can define the separator character yourself via sepChar if you want, but it's optional β€” the default is ",". Same deal with the quoting aka delimiting character: delChar is caller-definable but optional, and the default is '"'.

Now the tea on delMode: by default it's "auto", which means values only get wrapped in delChar when they actually need it. But when you set it to "always", the values are always surrounded by delChar instead. Periodt.

::csv::joinlist values ?sepChar? ?delChar? ?delMode?

Takes a list of lists of values and returns a string in CSV format containing all of them. Separator character? Caller-definable, optional, default ",". Quoting character? Also caller-definable, also optional, default '"'. And delMode works exactly like above: "auto" by default so values only get wrapped in delChar when needed, but "always" wraps them every time, no exceptions.

Here's the structure, bestie: every element of the outer list is considered a record, and those get separated by newlines in the result. The elements of each record are formatted as usual β€” through ::csv::join, naturally. πŸ’…

::csv::joinmatrix matrix ?sepChar? ?delChar? ?delMode?

Takes a matrix object following the API specified for the struct::matrix package and returns a string in CSV format containing those values. You guessed it: sepChar is optional, default ",". delChar is optional, default ". And delMode is "auto" by default β€” surrounding values with delChar only when needed β€” while "always" means values are always surrounded by the delChar instead.

Each row of the matrix counts as a record, and those are separated by newlines in the result. The elements of each record are formatted as usual (via ::csv::join). πŸ¦‹

::csv::read2matrix ?-alternate? chan m {sepChar ,} {expand none}

A wrapper around ::csv::split2matrix (peep below πŸ‘‡) that reads CSV-formatted lines from the specified channel chan β€” all the way until EOF, she's committed β€” and adds them to the given matrix m. Wanna know what expand is doing? Go read ::csv::split2matrix. πŸŽ€

::csv::read2queue ?-alternate? chan q {sepChar ,}

A wrapper around ::csv::split2queue (again, below) that reads CSV-formatted lines from the specified channel chan until EOF and adds them to the given queue q. Slay.

::csv::report cmd matrix ?chan?

A report command you can hand to the matrix methods format 2string and format 2chan. For the latter one, this command delegates the work to ::csv::writematrix. cmd is expected to be either printmatrix or printmatrix2channel. The channel argument chan has to be present for the latter and must not be present for the first. Yes, she's picky about that. πŸ™ˆ

::csv::split ?-alternate? line ?sepChar? ?delChar?

Converts a line in CSV format into a list of the values contained in that line. The character used to separate the values from each other can be defined by the caller via sepChar, but it's optional β€” default is ",". The quoting character can also be defined by the caller, optional again β€” default is '"'.

If the option -alternate is specified, a slightly different syntax is used to parse the input. That syntax is explained below in the FORMAT section. πŸ“–

::csv::split2matrix ?-alternate? m line {sepChar ,} {expand none}

The same as ::csv::split, except it appends the resulting list as a new row to the matrix m, using the method add row. The expansion mode specified via expand decides how the command deals with a matrix that has fewer columns than line contains. The allowed modes are:

  • 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. (Not me being dramatic, that's literally what it does.)
  • 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 none was set.
  • auto β€” In this mode the command expands the matrix as needed to hold all columns contained in line. The overall effect: after adding a series of lines, the matrix will have enough columns to hold all columns of the longest line encountered so far. Main character energy honestly.

::csv::split2queue ?-alternate? q line {sepChar ,}

The same as ::csv::split, but it appends the resulting list as a single item to the queue q, using the method put. Simple, clean, iconic. πŸ’–

::csv::writematrix m chan ?sepChar? ?delChar?

A wrapper around ::csv::join that takes all rows in the matrix m and writes them CSV formatted into the channel chan. 🫢

::csv::writequeue q chan ?sepChar? ?delChar?

A wrapper around ::csv::join that takes all items in the queue q (it assumes they are lists, okay? that's the deal) and writes them CSV formatted into the channel chan. ✨


🌈 FORMAT 🌈

The format of regular CSV files is specified as:

  1. 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.
  2. 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.
  3. If a value contains the character ", that character is represented by "".
  4. 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.

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's 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. That's the whole plot twist.

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


πŸ“ EXAMPLE πŸ“

Using the regular format, the record

123,"123,521.2","Mary says ""Hello, I am Mary""",""

is parsed into the items

  1. 123
  2. 123,521.2
  3. Mary says "Hello, I am Mary"
  4. "

Using the alternate format the result is

  1. 123
  2. 123,521.2
  3. Mary says "Hello, I am Mary"
  4. (the empty string)

instead. As you can see, only item (d) is different β€” now the empty string instead of a ". Literally just that one thing. 😭


πŸ’€ BUGS, IDEAS, FEEDBACK πŸ’€

This document, and the package it describes, will undoubtedly contain bugs and other problems. (She's humble, she's honest, she's growing.) Please report such in the category csv of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation β€” bestie, we want your thoughts.

When proposing code changes, please provide unified diffs, i.e. the output of diff -u.

Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. (Yes, that exact button. Go get it. πŸ’…)


🧚 SEE ALSO 🧚

matrix, queue


⭐ KEYWORDS ⭐

csv, matrix, package, queue, tcllib


πŸŽ€ CATEGORY πŸŽ€

Text processing


Copyright (c) 2002-2015 Andreas Kupries <andreas_kupries@users.sourceforge.net>


okay that's the whole page, bestie!! this one shipped as part of tcllib, version 0.8.1, and it lives in the manual section for csv(3tcl). go forth and parse some commas, you absolute icon πŸ¦‹βœ¨πŸ’–

πŸ“¦ where this page comes from