csv(n)
π 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 thestruct::matrixpackage 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 methodsformat 2stringandformat 2chan. For the latter, this command delegates the work to::csv::writematrix. cmd is expected to be eitherprintmatrixorprintmatrix2channel. 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 methodadd 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 modenonewas 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 methodput. π::csv::writematrix m chan ?sepChar? ?delChar?β a wrapper around::csv::jointhat 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::jointhat 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:
- 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. π - 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. - If a value contains the character
", that character is represented by"". - 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
123123,521.2Mary says "Hello, I am Mary""
Using the alternate format the result is
123123,521.2Mary says "Hello, I am Mary"- (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 π
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
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2002-2015 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms