Ir al contenido

Leer un archivo CSV ​

Traducción automática

Esta página se tradujo automáticamente del inglés y todavía no la revisó un hablante nativo; las correcciones son bienvenidas en GitHub. Si algo no coincide, vale el original en inglés.

Tarea: mostrar los clientes activos de un archivo CSV, del más antiguo al más nuevo.

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

Las columnas declaradas tienen tipo (true, 1, yes y on son un booleano verdadero); las demás llegan como texto. q:transform ejecuta sus operaciones en orden.

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

Ver DATA-1 y DATA-3, y Data Import (en inglés).

Licencia MIT · Hecho con VitePress