KScript 문서
KO

언어

영어에서 자동 번역되었습니다. 원문은 영어입니다.

csv 필드와 메서드

필드는 값에 관한 단순한 정보로, 대괄호 없이 점으로 읽어요: $value.name. 메서드는 인수를 받거나 작업을 수행하며, ->로 호출하거나 $value->name(arguments)처럼 값을 파이프로 넘겨서 호출해요: $value |> name(arguments).

필드

필드타입설명
.headerslist첫 줄에 있는 이름입니다. 예를 들어 fruit,price로 시작하는 csv라면 ['fruit', 'price']입니다. 첫 줄이 모두 숫자이면 그 줄도 데이터로 보므로 비어 있습니다.

KScript.Core에서 제공해요.

#csv->cells(row?: number, rows?: bool, column?: any, columns?: bool, header?: bool, as?: type) → any

셀들입니다. 단독으로 쓰거나 rows와 함께 쓰면 모든 행을, columns와 함께 쓰면 모든 열을 가져옵니다. row: 는 행 하나를 0부터 세어 고릅니다(-1은 마지막 행). column: 은 헤더 이름이나 0부터 세는 번호로 열 하나를 고릅니다. 행과 열을 함께 지정하면 둘이 만나는 셀 하나를 가져오고, rows와 column: 을 함께 쓰면 모든 행에서 그 열의 셀을 가져옵니다. 행은 첫 줄을 키로 하는 맵이며, 헤더가 없으면 셀의 리스트입니다. 열은 헤더를 키로 하는 리스트들의 맵이며, 헤더가 없으면 리스트들의 리스트입니다. 헤더가 없을 때 행의 길이는 서로 다를 수 있으며, 열을 가져올 때는 그 열에 닿지 않는 짧은 행을 건너뜁니다. 첫 줄은 모든 셀이 숫자가 아닌 한 헤더로 봅니다. 선택하려면 header 또는 not header를 지정하세요. 숫자로 적힌 셀은 숫자로 반환합니다. 셀은 쉼표, 탭, 세미콜론, 또는 숫자 사이의 공백으로 나뉠 수 있습니다. as: 에 타입을 지정하면 결과를 검사하고 그 타입으로 변환합니다.
매개변수받는 값
row 선택 사항number
rows 선택 사항플래그입니다: rows 또는 not rows
column 선택 사항any
columns 선택 사항플래그입니다: columns 또는 not columns
header 선택 사항플래그: header 또는 not header
as 선택type

인수에 이름을 붙일 수 있습니다: ->cells(…, rows: …, column: …).

예시
<let name="prices" value="```csv
  fruit,price
  apple,3
  plum,5
  ```" />
<!-- 'apple': the header line names each row's keys -->
<echo>{ $prices->cells(row: 0).fruit }</echo>
<!-- [3, 5]: the cells under the price header, as numbers -->
<echo>{ $prices->cells(column: 'price') }</echo>
<!-- 5: row 1 is plum, and its price is 5 -->
<echo>{ $prices->cells(row: 1, column: 'price') }</echo>
<!-- { fruit: ['apple', 'plum'], price: [3, 5] }: every column, keyed by its header -->
<echo>{ $prices->cells(columns) }</echo>
<let name="readings" value="```csv
  387
  942
  945
  ```" />
<!-- 2274: every line is a number, so there's no header and every line is a row -->
<echo>{ $readings->cells(column: 0)->sum() }</echo>
<let name="puzzle" value="```csv
  -903 -642 446
  200 818
  ```" />
<!-- [-903, -642, 446]: spaces split the numbers on each line -->
<echo>{ $puzzle->cells(row: 0) }</echo>
<!-- [[-903, 200], [-642, 818], [446]]: the second row is too short for the third column -->
<echo>{ $puzzle->cells(columns) }</echo>
$prices = ```csv
  fruit,price
  apple,3
  plum,5
  ```
// 'apple': the header line names each row's keys
echo $prices->cells(row: 0).fruit
// [3, 5]: the cells under the price header, as numbers
echo $prices->cells(column: 'price')
// 5: row 1 is plum, and its price is 5
echo $prices->cells(row: 1, column: 'price')
// { fruit: ['apple', 'plum'], price: [3, 5] }: every column, keyed by its header
echo $prices->cells(columns)
$readings = ```csv
  387
  942
  945
  ```
// 2274: every line is a number, so there's no header and every line is a row
echo $readings->cells(column: 0)->sum()
$puzzle = ```csv
  -903 -642 446
  200 818
  ```
// [-903, -642, 446]: spaces split the numbers on each line
echo $puzzle->cells(row: 0)
// [[-903, 200], [-642, 818], [446]]: the second row is too short for the third column
echo $puzzle->cells(columns)

#csv->save(path: string)

CSV를 파일로 써요. 경로에 확장자가 없으면 .csv를 붙여요.
매개변수받는 값
path 필수string
예시
<let name="prices" value="```csv
  fruit,price
  apple,3
  plum,5
  ```" />
<!-- writes prices.csv: the name has no extension, so .csv is added -->
<do>$prices->save('prices')</do>
$prices = ```csv
  fruit,price
  apple,3
  plum,5
  ```
// writes prices.csv: the name has no extension, so .csv is added
$prices->save('prices')

any의 메서드

string의 메서드