跳到正文

读取 CSV 文件 ​

机器翻译

本页由英文原文机器翻译而来,尚未经过母语审校,欢迎在 GitHub 上提出修改。内容如有出入,以英文原文为准。

任务: 显示一个 CSV 文件中的活跃客户,从最早的开始。

yaml
# Recipe: read a CSV file, typed, filtered and sorted — no database.
paths:
  components: ./components
text
id,name,age,active
1,Ana,28,true
2,Bruno,35,true
3,Carla,42,false
4,Davi,19,yes

声明过的列带有类型(true、1、yes 和 on 都是布尔值真);其余的列以文本形式到达。 q:transform 按顺序执行它的操作。

xml
<q:component name="Customers">
  <!-- The path is relative to the app's folder. Declared columns are typed;
       the others come as text. -->
  <q:data name="customers" source="import/customers.csv" type="csv">
    <q:column name="id" type="integer" />
    <q:column name="age" type="integer" />
    <q:column name="active" type="boolean" />
    <q:transform>
      <q:filter condition="active" />
      <q:sort by="age" order="desc" />
    </q:transform>
  </q:data>

  <ui:window title="Active customers">
    <ui:table source="{customers}">
      <ui:column key="name" label="Name" />
      <ui:column key="age" label="Age" />
    </ui:table>
  </ui:window>
</q:component>
xml
<q:test name="the active customers, oldest first" page="/">
  <test:visit />
  <test:expect text="Bruno 35 Ana 28 Davi 19" />
  <test:expect no-text="Carla" />
</q:test>
text
tests/customers.test.q
  PASS  the active customers, oldest first
1 passed, 0 failed

参见 DATA-1 和 DATA-3,以及 Data Import(英文)。

MIT 许可证 · 使用 VitePress 构建