Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library

    std.csv.parse

    Parses CSV text into positional string rows.

    parse(text: string) -> Result<Array<Array<string>>, string>
    Module
    std.csv
    Receiver
    Not applicable
    Minimum arity
    1
    Maximum arity
    1

    Declaration and parameters

    Link

    Availability

    Included by default: Pending

    Import std.csv in language mode 5.21.

    The current compiler selects std/csv.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Source evidence

    Receiver

    Parameter constraints

    parseWithHeader returns Result<Array<Map<string, string>>, string>. A repeated header name returns Err instead of choosing a first or last column.

    Header uniqueness is checked before converting data rows. Repeated names are rejected when cell values are equal and when the input contains a header without data rows.

    The header names are the parsed CSV field values. Equality uses those string values without an additional trimming or case-folding step.

    Use csv.parse when columns must remain positional. Positional parsing preserves every field and does not assign header names.

    Result

    The declared result type is Result<Array<Array<string>>, string>.

    Effects

    Failures

    Result and effects

    Link

    Parses CSV text into positional string rows.

    Related entries

    Link

    std.csv

    Require unique headers for named CSV rows

    Link
    Ruletopaz.stdlib.contract.csv-unique-headers.rule

    Require unique headers for named CSV rows

    parseWithHeader returns Result<Array<Map<string, string>>, string>. A repeated header name returns Err instead of choosing a first or last column.

    Header uniqueness is checked before converting data rows. Repeated names are rejected when cell values are equal and when the input contains a header without data rows.

    The header names are the parsed CSV field values. Equality uses those string values without an additional trimming or case-folding step.

    Use csv.parse when columns must remain positional. Positional parsing preserves every field and does not assign header names.

    Why

    A named row gives each name one column. Rejecting duplicates keeps column order from selecting a winner, while positional parsing retains data that has no unique names.

    Source evidence

    Read unique named columns

    Link
    main.tpzRead unique named columnsOpen in Playground
    1. import std.csv
    2. import std.test
    3. import std.io
    4. let rows = test.assertOk(csv.parseWithHeader("name,count\nTopaz,6"))
    5. let row = test.assertSome(rows.get(0))
    6. io.writeLine(test.assertSome(row.get("name")))
    7. io.writeLine(test.assertSome(row.get("count")))
    Expected outputexit 0
    Topaz
    6
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. Topaz
    2. 6
    Source evidence

    Reject duplicate names

    Link
    main.tpzReject duplicate namesOpen in Playground
    1. import std.csv
    2. import std.io
    3. match (csv.parseWithHeader("name,name\nleft,right")) {
    4. case Err(_) => io.writeLine("duplicate headers rejected")
    5. case Ok(_) => io.writeLine("accepted")
    6. }
    Expected outputexit 0
    duplicate headers rejected
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. duplicate headers rejected

    The parser rejects this input by returning Err. The program handles that result and exits successfully.

    Source evidence

    Reject duplicate names after reversing values

    Link
    main.tpzReject duplicate names after reversing valuesOpen in Playground
    1. import std.csv
    2. import std.io
    3. match (csv.parseWithHeader("name,name\nright,left")) {
    4. case Err(_) => io.writeLine("duplicate headers rejected")
    5. case Ok(_) => io.writeLine("accepted")
    6. }
    Expected outputexit 0
    duplicate headers rejected
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. duplicate headers rejected

    The parser rejects this input by returning Err. The program handles that result and exits successfully.

    Source evidence

    Reject duplicate names even when the values agree

    Link
    main.tpzReject duplicate names even when the values agreeOpen in Playground
    1. import std.csv
    2. import std.io
    3. match (csv.parseWithHeader("name,name\nsame,same")) {
    4. case Err(_) => io.writeLine("duplicate headers rejected")
    5. case Ok(_) => io.writeLine("accepted")
    6. }
    Expected outputexit 0
    duplicate headers rejected
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. duplicate headers rejected

    The parser rejects this input by returning Err. The program handles that result and exits successfully.

    Source evidence

    Reject duplicate names before reading data rows

    Link
    main.tpzReject duplicate names before reading data rowsOpen in Playground
    1. import std.csv
    2. import std.io
    3. match (csv.parseWithHeader("name,name")) {
    4. case Err(_) => io.writeLine("duplicate headers rejected")
    5. case Ok(_) => io.writeLine("accepted")
    6. }
    Expected outputexit 0
    duplicate headers rejected
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. duplicate headers rejected

    The parser rejects this input by returning Err. The program handles that result and exits successfully.

    Source evidence

    Preserve repeated header text in positional rows

    Link
    main.tpzPreserve repeated header text in positional rowsOpen in Playground
    1. import std.csv
    2. import std.test
    3. import std.io
    4. let rows = test.assertOk(csv.parse("name,name\nleft,right"))
    5. let row = test.assertSome(rows.get(1))
    6. io.writeLine("{row[0]}:{row[1]}")
    Expected outputexit 0
    left:right
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. left:right
    Source evidence

    Menu

    Download