csv(n)

πŸ“¦
version 2.0 Β· 2 versions
, in fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– csv(n) β€” CSV processing πŸ’–

okay bestie, welcome to csv(n) β€” the Tcl manual page for the csv package, a.k.a. everything you ever wanted to know about wrangling CSV (Comma Separated Values) data without losing your mind. πŸŽ€


✨ 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 🌈

The csv package gives you commands to manipulate info in CSV FORMAT (CSV = Comma Separated Values). That's it, that's the whole vibe. ✨


πŸ‘‘ COMMANDS πŸ‘‘

The following commands are available:

  • ::csv::iscomplete data β€” a predicate, bestie. It checks whether the argument data is a complete csv record. The result is a boolean flag saying whether the data is complete. The result is true if the data is complete. πŸ’…
  • ::csv::join values ?sepChar? ?delChar? ?delMode? β€” takes a list of values and hands you back a string in CSV format containing those values. The separator character can be defined by the caller, but it's optional β€” default is ",". The quoting aka delimiting character can also be defined by the caller, also optional β€” default is '"'. By default the quoting mode delMode is "auto", which surrounds values with delChar only when needed. When set to "always" though? Values are always surrounded by the delChar. no in-between. πŸ¦‹
  • ::csv::joinlist values ?sepChar? ?delChar? ?delMode? β€” takes a list of lists of values and returns a string in CSV format containing those values. Separator char definable by caller, optional, default ",". Quoting character definable by caller, optional, default '"'. By default the quoting mode delMode is "auto", surrounding values with delChar only when needed; set it to "always" and values are always surrounded by the delChar instead. Each 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 β€” via ::csv::join. 🫢
  • ::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 these values. Separator char definable by caller, optional, default ",". Quoting character definable by caller, optional, default '"'. By default the quoting mode delMode is "auto", surrounding values with delChar only when needed; set to "always" and values are always surrounded by the delChar instead. Each row of the matrix is considered a record, 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 (see below) that reads CSV-formatted lines from the specified channel chan (until EOF) and adds them to the given matrix m. For an explanation of the expand argument, go peek at ::csv::split2matrix. πŸŽ€
  • ::csv::read2queue ?-alternate? chan q {sepChar ,} β€” a wrapper around ::csv::split2queue (see below) that reads CSV-formatted lines from the specified channel chan (until EOF) and adds them to the given queue q. 🧚
  • ::csv::report cmd matrix ?chan? β€” a report command which can be used by the matrix methods format 2string and format 2chan. For the latter, 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. iconic gatekeeping, honestly. πŸ’…
  • ::csv::split ?-alternate? line ?sepChar? ?delChar? β€” converts a 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 sepChar, but it's optional β€” default is ",". The quoting character can be defined by the caller, but it's optional too β€” 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 section FORMAT. πŸ‘€

  • ::csv::split2matrix ?-alternate? m line {sepChar ,} {expand none} β€” the same as ::csv::split, but it appends the resulting list as a new row to the matrix m, using the method add row. The expansion mode specified via expand determines how the command handles a matrix with fewer columns than are contained in line. The allowed modes are:
    • none β€” this is the default mode. In this mode it's the responsibility of the caller to ensure the matrix has enough columns to contain the full line. If there aren't enough columns, the list of values is silently truncated at the end to fit. πŸ’€
    • 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 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. main character energy. πŸ”₯
  • ::csv::split2queue ?-alternate? q line {sepChar ,} β€” the same as ::csv::split, but appending the resulting list as a single item to the queue q, using the method put. πŸ“
  • ::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) 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 isn't important for this case, since 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. one tiny difference and it changes EVERYTHING, iykyk.

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 can be seen, only item (d) is different β€” now the empty string instead of a ". 🫢


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

This document, and the package it describes, will undoubtedly contain bugs and other problems. not me being self-aware 😭 Please report such in the category csv of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist](http://core.tcl.tk/tcllib/reportlist). Please also report any ideas for enhancements you may have for either package and/or documentation.

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. πŸ“Ž

🌈 SEE ALSO 🌈

matrix, queue


πŸŽ€ KEYWORDS πŸŽ€

csv, matrix, package, queue, tcllib


πŸ‘‘ CATEGORY πŸ‘‘

Text processing


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


and that's the whole page, bestie! this one's from tcllib, version 0.8.1, and it's the csv(n) manual page β€” go forth and split, join, and absolutely slay those comma-separated values. periodt. πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from