Skip to main content

CSV (object)​

Serializes or deserializes CSV data.

The CSV object contains a parse function and a stringify function which can be used to parse strings of CSV (comma-separated values) data into arrays-of-arrays-of-strings and serialize arrays-of-arrays-of-strings into strings of CSV data.

Its interface is similar to JSON.parse and JSON.stringify, but CSV does not support the spacing/replacer/reviver options that JSON.parse and JSON.stringify have.

Example

const rows = CSV.parse("name,age\nAlice,30\n");
rows.push(["Bob", "25"]);
console.log(CSV.stringify(rows));
const CSV: {
parse(input: string): Array<Array<string>>;
stringify(input: Array<Array<string>>): string;
};

CSV.parse (method)​

Parse a CSV string into an Array of Arrays of strings.

The outer array holds the rows, and the inner arrays hold the items in each row.

Example

const rows = CSV.parse(readFile("data.csv"));
for (const [name, age] of rows) {
console.log(name, age);
}
parse(input: string): Array<Array<string>>;

CSV.stringify (method)​

Convert an Array of Arrays of strings into a CSV string.

The outer array holds the rows, and the inner arrays hold the items in each row.

Example

const csv = CSV.stringify([
["name", "age"],
["Alice", "30"],
]);
writeFile("people.csv", csv);
stringify(input: Array<Array<string>>): string;