본문으로 건너뛰기
토파즈6.0 · 출시 전
다운로드 →

문서 검색

명세 6.0

방향키로 결과를 고르고 Enter를 눌러 여세요.

API 이름, 진단 번호, 규칙 또는 프로젝트 주제를 입력하세요.

    라이브러리

    std.csv.parseWithHeader

    Parses CSV text into rows indexed by header names.

    parseWithHeader(text: string) -> Result<Array<Map<string, string>>, string>
    수신자
    해당 없음
    최소 인자 수
    1
    최대 인자 수
    1

    Declaration and parameters

    링크

    사용 조건

    기본 포함 · 미정

    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.

    수신자

    매개변수 제약

    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.

    결과

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

    효과

    실패

    Result and effects

    링크

    Parse named CSV rows after checking that every header name is unique. Repeated names return Err before any data row is converted, including a header with no following data rows.

    Why

    링크

    Each name identifies one column. Rejecting a repeated name prevents column order from choosing which value survives.

    Related entries

    링크

    std.csv →

    Require unique headers for named CSV rows

    링크
    규칙topaz.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.

    이유

    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.

    Read unique named columns

    링크
    main.tpzRead unique named columns플레이그라운드 열기 →
    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")))
    기대 출력종료 코드 0
    Topaz
    6
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. Topaz
    2. 6

    Reject duplicate names

    링크
    main.tpzReject duplicate names플레이그라운드 열기 →
    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. }
    기대 출력종료 코드 0
    duplicate headers rejected
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. duplicate headers rejected

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

    Reject duplicate names after reversing values

    링크
    main.tpzReject duplicate names after reversing values플레이그라운드 열기 →
    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. }
    기대 출력종료 코드 0
    duplicate headers rejected
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. duplicate headers rejected

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

    Reject duplicate names even when the values agree

    링크
    main.tpzReject duplicate names even when the values agree플레이그라운드 열기 →
    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. }
    기대 출력종료 코드 0
    duplicate headers rejected
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. duplicate headers rejected

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

    Reject duplicate names before reading data rows

    링크
    main.tpzReject duplicate names before reading data rows플레이그라운드 열기 →
    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. }
    기대 출력종료 코드 0
    duplicate headers rejected
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. duplicate headers rejected

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

    Preserve repeated header text in positional rows

    링크
    main.tpzPreserve repeated header text in positional rows플레이그라운드 열기 →
    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]}")
    기대 출력종료 코드 0
    left:right
    
    $ topaz run main.tpz · 대상 제품 5.21.3
    기대 결과

    기대 결과

    실행을 마칩니다. 종료 코드 0.

    기대 표준 출력
    1. left:right

    메뉴

    다운로드 →