Fork me on GitHub

src/arraymancer/io/io_csv

  Source Edit

Procs

proc read_csv[T: SomeNumber | bool | string](csvPath: string;
    skipHeader = false; separator = ','; quote = '\"'): Tensor[T] {.noinit.}

Load a csv into a Tensor. All values must be of the same type.

If there is a header row, it can be skipped.

The reading of CSV files currently does not handle parsing a tensor created with toCsv. This is because the dimensional information becomes part of the CSV output and the parser has no option to reconstruct the correct tensor shape. Instead of a NxMx...xZ tensor we always construct a NxM tensor, where N-1 is the rank of the original tensor and M is the total size (total number of elements) of the original tensor!

Input:

  • csvPath: a path to the csvfile
  • skipHeader: should read_csv skip the first row
  • separator: a char, default ','
  • quote: a char, default '"' (single and double quotes must be escaped). Separators inside quoted strings are ignored, for example: "foo", "bar, baz" corresponds to 2 columns not 3.
  Source Edit
proc to_csv[T](tensor: Tensor[T]; csvPath: string; separator = ',')

Stores a tensor in a csv file. Can handle tensors of arbitrary dimension by using a schema (= csv columns) of

dimension_1, dimension_2, ..., dimension_(tensor.rank), value

where the 'dimension_i' columns contain indices, and the actual tensor values are stored in the 'value' column.

For example the tensor @[@[1, 2, 3], @[4, 5, 6]].toTensor() is stored as:

dimension_1,dimension_2,value 0,0,1 0,1,2 0,2,3 1,0,4 1,1,5 1,2,6

Input:

  • tensor: the tensor to store
  • csvPath: output path of the csvfile
  • separator: a char, default ','
  Source Edit
Arraymancer Technical reference Tutorial Spellbook (How-To's) Under the hood