
CSV
Parsing and stringifying of Comma Separated Values (CSV) data.
The parsers map CSV data to an array of objects, with field names corresponding to the header. For example, the CSV data:
name, valueA, 10B, 2C, 3
maps to the following array of objects:
[ { "name": "A", "value": 10 }, { "name": "B", "value": 2 }, { "name": "C", "value": 3 }]
The def function automatically parses and stringifies CSV data to a Markdown table (it also works for XLSX).
def("DATA", env.files[0])
def
also supports basic row filtering options that control how many rows you want to insert into the prompt.
def("DATA", env.files[0], { sliceHead: 50, // take first 50 sliceTail: 25, // take last 25 sliceSample: 5, // take 5 at random})
Similarly to the JSON
class in JavaScript, the CSV
class provides methods to parse and stringify comma-separated values (CSV) data.
The parse
method converts a CSV string into an array of objects. The first row is used as the header row.
const csv = await workspace.readText("penguins.csv")const rows = CSV.parse(csv)
If the CSV file does not have a header row, you can specify the column names as an array of strings. You can also specify a custom data separator.
const rows = CSV.parse(csv, { delimiter: "|", headers: ["name", "value"],})
You can use defData to serialize the rows
object to the prompt. defData
also supports basic row filtering options like def
.
defData("DATA", rows)
stringify
Section titled “stringify”The stringify
method converts an array of objects to a CSV string.
const csvString = CSV.stringify(rows)
The markdownify
method converts an array of objects into a Markdown table. This encoding is more efficient with LLM tokenizers.
const md = CSV.markdownify(rows)
| name | value ||------|-------|| A | 10 || B | 2 || C | 3 |
parsers
Section titled “parsers”The parsers also provide a parser for CSV. It returns undefined
for invalid inputs and supports files and parsing options.
const rows = parsers.CSV(env.files[0])
Repair
Section titled “Repair”You can specify the repair: true
option to fix common LLM mistakes around CSV.
const rows = CSV.parse(csv, { repair: true })