2025-01-18 23:11:55 +00:00
|
|
|
# CSV Module
|
|
|
|
|
|
|
|
Functions for operating over CSV data.
|
|
|
|
|
|
|
|
### each-record
|
|
|
|
|
|
|
|
```
|
|
|
|
csv:each-record FILE BLOCK
|
|
|
|
```
|
|
|
|
|
|
|
|
Opens the CSV file at FILE and calls BLOCK on each record. It is expected that this
|
|
|
|
CSV file has a header which appears as the first row. This command will read and
|
|
|
|
index the header, then start calling BLOCK from the row directly below the header.
|
|
|
|
|
|
|
|
BLOCK is called with the arguments _|row header|_ where:
|
|
|
|
|
|
|
|
- _row_ contains the fields of the current row as a list.
|
|
|
|
- _header_ contains a hash mapping a header name to a field index.
|
|
|
|
|
|
|
|
The return value will be nil.
|
|
|
|
|
|
|
|
```
|
|
|
|
csv:each-record "winds.csv" { |row hdr|
|
|
|
|
set name $row.($hdr.name)
|
2025-01-19 05:01:00 +00:00
|
|
|
set bearing $row.($hdr.bearing)
|
2025-01-18 23:11:55 +00:00
|
|
|
|
2025-01-19 05:01:00 +00:00
|
|
|
echo "Wind $name has bearing $bearing"
|
2025-01-18 23:11:55 +00:00
|
|
|
}
|
2025-02-02 23:28:44 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
```
|
|
|
|
csv:to-csv CONT [-header HEADER]
|
|
|
|
```
|
|
|
|
|
|
|
|
Produces a CSV using the items of CONT and writes it as a string. CONT must be a list of iterator of hashable
|
|
|
|
elements.
|
|
|
|
|
|
|
|
If HEADER is defined, it must be a list of strings identifying the name and order of the hash keys to read
|
|
|
|
from each hashable item of CONT. If HEADER is not defined, then the keys of the first consumed hashable item will be
|
|
|
|
used, with the keys sorted in alphabetical order.
|