CSV Parser

Syntax

	$input.parseCSV()

Description

Interprets the input as CSV formatted text.

Parameters

Name Name Type Type Description Mandatory Default
Input Binary value Text in CSV format Yes -
parsers map Map with column name as key and (TLScript) function as value. The values in the respective cells are formatted according to the specified function. No null
columnSeparator Char Separator for column values No ';'
lineSeparator String Separator for the individual lines No "\n"
quoteChar Char

Character used to quote values that contain separators or line breaks. With \ (e.g. \") the quoting character becomes a normal character.

No '"'
trimSpaces Boolean Whether spaces at the beginning or end of values should be ignored. No false
raw Boolean

Whether the function should return the raw data as a list of lists. Otherwise, the function returns a list of maps, using the column names (from the header) as keys.

No false/null

Return value

Type: List

Creates a list/map for each line in the input and then combines these lists into one list.

Examples

Example 1:

{
    toParse = binary(
        name: "test.csv",
        data: "date; name; number
        15.07.2024; test; 5
        16.07.2024; test2; 2; etc.
        17.07.2024",
        contentType: "text/csv",
        encoding: "utf-8"
    );
    result = $toParse.parseCSV();
    $result
}

Output: [{date=" 15.07.2024", number=" 5", name=" test"}, {date=" 16.07.2024", number=" 2", name=" test2"}, {date=" 17.07.2024", number=, name=}]

Calling parseCSV() returns a list of maps. The first line is selected as the header and its entries are used as the key for the maps. Subsequently, for each additional line, the map is generated from its entries according to the keys of the header of the corresponding map, whereby excess entries of the line are ignored and missing entries of the line are filled with empty strings.

Example 2:

{
    toParse = binary(
        name: "test.csv",
        data: "date; name; number
        15.07.2024; test; 5
        16.07.2024; test2; 2; etc.
        17.07.2024",
        contentType: "text/csv",
        encoding: "utf-8"
    );
    result = $toParse.parseCSV(trimSpaces:true, 
      parsers: {
        "date": x-> dateFormat("dd.MM.yy").format(dateFormat("dd.MM.yyy").parse($x)), 
        "number": y -> numberFormat("#").parse($y)+1 
      });
    $result
}

Output: [{date=15.07.24, number=6.0, name="test"}, {date=16.07.24, number=3.0, name="test2"}, {date=17.07.24, number=, name=}]

When calling parseCSV, a parser for the column with the name "date" and a parser for the column with the name "number" is specified here, which prepares the entries from the corresponding cells.

Example 3:

{
    toParse = binary(
        name: "test.csv",
        data: "date; name; number
        15.07.2024; test; 5
        16.07.2024; test2; 2; etc.
        17.07.2024",
        contentType: "text/csv",
        encoding: "utf-8"
    );
    result = $toParse.parseCSV(raw:true);
    $result[0]
}

Output: ["date", "name", "number"]

Calling parseCSV(raw:true) returns the list [["date", " name", " number"], [" 15.07.2024", " test", " 5"], [" 16.07.2024", " test2", " 2", " etc."],[" 17.07.2024"]]. So with spaces before every value except the first ("date"). If these spaces are not required, you must callparseCSV(raw:true, trimSpaces:true).