Skip to content

Decoders

.qsp.decode.csv

Decodes CSV data from strings or bytes with delimiter separated values into tables or matrices of data with desired data types.

Signatures:

 .qsp.decode.csv[schema; delimiter]

     - `schema`      - Desired schema of output data. The schema object can be represented
                       as a table with the desired output columns. Optionally, the schema
                       can simply be a list of types to support as type characters.
     - `[delimiter]` - The delimiter character is the field separator for the records in
                       the encoded data. This field is optional and will default to a
                       comma.
 .qsp.decode.csv[opts]

     - `opts.schema`     - Desired schema of output data. The schema object can be represented
                           as a table with the desired output columns. Optionally, the schema
                           can simply be a list of types to support as type characters.
     - `opts.delimiter`  - Record delimiter for values in the delimited file. This value
                           defaults to a comma if not provided.
     - `opts.header`     - Indicates if the data to be decoded has a header or not. A header is
                           a single record at the top of the delimited data indicating column
                           names for the data. Valid options include `` `always `` if
                           the data has a header.  If the data does not have a header
                           `` `none `` can be provided. By default it is assumed data
                           has a header.
     - `opts.exclude`    - A symbol list of column names or indices of the columns to ignore
                           when decoding the data.

Example: CSV Decode from a File

 // Generate a random table of data and store it in an inventory file.
 n: 10000;
 t: ([]
  date: n?.z.d + neg til 10;
  price: (n?1000)div 100;
  item: n?`3;
  description: {rand[100]?.Q.an}each til n;
  quantity: n?10000);
 `:/tmp/inventory.csv 0: csv 0: t;

 // Read and parse the data from a file
 schema: ([] date: `date$(); price:`int$(); item:`symbol$(); description: (); quantity:`long$());
 .qsp.run
     .qsp.read.fromFile["/tmp/inventory.csv"]
     .qsp.decode.csv[schema]
     .qsp.write.toConsole[]

 /=>                              | date       price item description                      quantity
 /=> -----------------------------| ---------------------------------------------------------------
 /=> 2021.07.16D19:45:03.929480200| 2021.07.16 7     ehm  "3qupWqmNh6y8TeTzJlW49NlRzv0_0"  2659
 /=> 2021.07.16D19:45:03.929480200| 2021.07.14 2     iif  "_eB_lq"                         8257
 /=> 2021.07.16D19:45:03.929480200| 2021.07.12 7     eod  "GhUgGe3PH9Ie2NOw"               3907
 /=> 2021.07.16D19:45:03.929480200| 2021.07.11 0     goj  "Dvmemf3H2P"                     6100
 /=> 2021.07.16D19:45:03.929480200| 2021.07.09 1     bpm  "GbSjldDmUprmfiBa0UI8I"          367
 /=> ..