Skip to main content

CSVLoader

Streaming loader for comma-separated value and delimiter-separated value encoded files.

LoaderCharacteristic
File FormatCSV
Data FormatTables
File TypeText
File Extension.csv, .tsv, .dsv
MIME Typestext/csv, text/tab-separated-values, text/dsv
Supported APIsload, parse, parseSync, parseInBatches

Usage

import {load} from '@loaders.gl/core';
import {CSVLoader} from '@loaders.gl/csv';

const data = await load(url, CSVLoader);
// or
const data = await load(url, CSVLoader, {csv: options});

A complication with the CSV format is that CSV files can come with or without an initial header line. While the CSVLoader will attempt to detect if the first line is a header, this can fail. If you know the format of the file you can use options.csv.header to specify how to handle the first line.

import {load} from '@loaders.gl/core';
import {CSVLoader} from '@loaders.gl/csv';

const data = await load(url_to_csv_with_header, CSVLoader, {csv: {header: true});
const data = await load(url_to_csv_without_header, CSVLoader, {csv: {header: false});

Options

OptionTypeDefaultDescription
csv.shapeStringobject-row-table'object-row-table' rows are objects (keyed by colum name). 'array-row-table' rows are arrays of values.
csv.headerBoolean | StringautoIf true, the first row of parsed data will be interpreted as field names. If false, the first row is interpreted as data.
csv.columnPrefixStringcolumnThe prefix to use when naming columns for CSV files with no header. Defaults to 'column1', 'column2' etc.
csv.delimiterStringauto-detectThe delimiting character.
csv.newlineStringauto-detectThe newline sequence. Must be \r, \n, or \r\n.
csv.quoteCharString"The character used to quote fields.
csv.escapeCharString"The character used to escape the quote character within a field.
csv.dynamicTypingBooleantrueIf true, numeric and boolean data values will be converted to their type (instead if strings. Note: if you disable dynamicTyping, you need to explicitly set header to a boolean value. Otherwise, header: 'auto' would automatically treat the first row as a header.
csv.commentsStringfalseComment indicator (for example, "#" or "//"). Lines starting with this string are skipped.
csv.skipEmptyLinesStringtrueIf true, lines that are completely empty (those which evaluate to an empty string) will be skipped. If set to 'greedy', lines that don't have any content (those which have only whitespace after parsing) will also be skipped.
csv.transformFunction-A function to apply on each value. The function receives the value as its first argument and the column number or header name when enabled as its second argument. The return value of the function will replace the value it received. The transform function is applied before dynamicTyping.
csv.transformFunction-A function to apply on each value and optionally column number or header name. returned value replaces. The transform function is applied before dynamicTyping.
csv.delimitersToGuessArray, \t | ;
csv.fastModeBooleanauto-detectForce set "fast mode". Fast mode speeds up parsing significantly for large inputs but only works when the input has no quoted fields. Fast mode will be auto enabled if no " characters appear in the input.