KScript 文档
ZH

类型与验证

CSV

一个 csv 值是包含有效 CSV 的文本:以逗号分隔单元格的行,每行一条记录。可以将其写成 ```csv 块,或为变量指定 csv 类型并使用带引号的文本。无论哪种方式,如果某一行不符合 CSV 格式(例如引号未闭合),都会在编写的位置报错,包括在编辑器中以及脚本运行时。

<const name="prices">```csv
    fruit,price
    apple,3
    plum,5
    ```</const>

<let name="totals" type="csv" value="'month,total'" />
const $prices = ```csv
  fruit,price
  apple,3
  plum,5
  ```

$totals: csv = 'month,total'

第一行通常是列名。.headers 会读取这些列:

<echo value="$prices.headers" /> <!-- ['fruit', 'price']: the first line names the columns -->
echo $prices.headers // ['fruit', 'price']: the first line names the columns

读取单元格

->cells() 读取 csv 的单元格。单独使用,或配合 rows 标志时,返回每一行;配合 columns 时,返回每一列。row: 选取一行,column: 选取一列。行号从 0 开始计数(-1 表示最后一行),列可以用表头名称指定,也可以从 0 开始按序号指定:

<echo value="$prices->cells()" /> <!-- [{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]: every row -->
<echo value="$prices->cells(columns)" /> <!-- { fruit: ['apple', 'plum'], price: [3, 5] }: every column, by its header -->
<echo value="$prices->cells(row: 0).fruit" /> <!-- 'apple': the header line names each row's keys -->
<echo value="$prices->cells(column: 'price')" /> <!-- [3, 5]: the cells under the price header -->
<echo value="$prices->cells(row: 1, column: 'price')" /> <!-- 5: where plum's row meets the price column -->
echo $prices->cells() // [{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]: every row
echo $prices->cells(columns) // { fruit: ['apple', 'plum'], price: [3, 5] }: every column, by its header
echo $prices->cells(row: 0).fruit // 'apple': the header line names each row's keys
echo $prices->cells(column: 'price') // [3, 5]: the cells under the price header
echo $prices->cells(row: 1, column: 'price') // 5: where plum's row meets the price column

row:和rows是二选一的,column:和columns也是二选一的:同时指定某一对中的两个会报错。每对中各选一个可以一起使用:->cells(rows, column: 'price')表示每一行的价格,与->cells(column: 'price')相同。单行与所有列不能同时使用,因为一行中的单元格已经属于某一列,因此->cells(row: 0, columns)会报错,并提示改用->cells(row: 0)。

写成数字的单元格会输出为数字,因此可以直接求和。其他单元格为文本,007 这类编码也是文本,因为其开头的零会丢失。

除非第一行的每个单元格都是数字,否则它是表头;若全是数字,则视为数据。谜题输入和读数通常没有表头,因此每一行都是一行数字。第一行可能是任一情况时,请使用 header 或 not header:

<const name="readings">```csv
    387
    942
    945
    ```</const>

<echo value="$readings->cells(column: 0)->sum()" /> <!-- 2274: every line is a number, so there's no header -->
<echo value="$prices->cells(not header)->count()" /> <!-- 3: the fruit,price line is a row too -->
const $readings = ```csv
  387
  942
  945
  ```

echo $readings->cells(column: 0)->sum() // 2274: every line is a number, so there's no header
echo $prices->cells(not header)->count() // 3: the fruit,price line is a row too

单元格由逗号分隔。当 csv 中没有逗号时,则改用制表符或分号分隔,数字之间的空格也可以作为分隔符。各行所含的单元格数量可以不同:

<const name="numbers">```csv
    -903 -642 -518 -585 446
    692 853
    ```</const>

<echo value="$numbers->cells(row: 0)" /> <!-- [-903, -642, -518, -585, 446]: spaces split the numbers -->
<echo value="$numbers->cells(row: -1)->sum()" /> <!-- 1545: the last row, 692 + 853 -->
<echo value="$numbers->cells(column: 2)" /> <!-- [-518]: the third number of each row long enough to have one -->
<echo value="$numbers->cells(columns)" /> <!-- [[-903, 692], [-642, 853], [-518], [-585], [446]] -->
const $numbers = ```csv
  -903 -642 -518 -585 446
  692 853
  ```

echo $numbers->cells(row: 0) // [-903, -642, -518, -585, 446]: spaces split the numbers
echo $numbers->cells(row: -1)->sum() // 1545: the last row, 692 + 853
echo $numbers->cells(column: 2) // [-518]: the third number of each row long enough to have one
echo $numbers->cells(columns) // [[-903, 692], [-642, 853], [-518], [-585], [446]]

没有表头时,列会跳过长度不够到达该列的行,因此 column: 2 会取出每个有该位置数据的行的第三个数字,而 columns 对后面的列返回的列表会更短。没有任何行能到达的列会报错。有表头时每一行都包含所有列,因此在这种情况下行长度不足就是错误。

为 as: 指定一个类型后,结果会被检查并转换为该类型。行会变成该类型的记录,其单元格会被转换为相应的字段类型:

<type name="Price">
  <field name="fruit" type="string" />
  <field name="price" type="int" />
</type>

<let name="cheapest" value="$prices->cells(as: Price)->min(by: fn(row) => $row.price)" />
<echo value="$cheapest.fruit" /> <!-- 'apple': 3 is the lowest price -->
type Price {
  fruit: string
  price: int
}

$cheapest = $prices->cells(as: Price)->min(by: fn(row) => $row.price)
echo $cheapest.fruit // 'apple': 3 is the lowest price

它不是文件

@read_csv 读取文件,因此需要一个file_path。像@read_csv($prices)那样传入 csv 文本会报错:"'path' 参数需要一个 file_path,但这里是 csv 文本,而不是文件。"其快速修复方法是改为由它自己读取 csv:

<echo value="$prices->cells()" /> <!-- the rows, read from the text itself -->
echo $prices->cells() // the rows, read from the text itself

->save()将其写入文件,当名称没有扩展名时添加.csv,然后用@read_csv读回该文件:

<do>$prices->save('prices')</do> <!-- writes prices.csv -->
$prices->save('prices') // writes prices.csv

Edit this page on GitHub