KScript 文档
ZH

csv 字段和方法

字段是关于值的一个普通事实,用点号读取,不需要方括号:$value.name。方法会接收参数或执行操作,调用方式为 ->:$value->name(arguments),也可以把值通过管道传入:$value |> name(arguments)。

字段

字段类型说明
.headerslist第一行的列名,例如 csv 以 fruit,price 开头时为 ['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 的方法