Skip to content

Latest commit

 

History

History
79 lines (62 loc) · 3.77 KB

csv_reader_task.md

File metadata and controls

79 lines (62 loc) · 3.77 KB

CsvReaderTask

Reads a CSV file, whose path is set in the options, and iterates over its lines, outputting each line as an associative array indexed by the CSV headers.

Task reference

  • Service: CleverAge\ProcessBundle\Task\File\Csv\CsvReaderTask
  • Code: csv_reader (task: csv_reader)
  • Iterable task

Accepted inputs

Input is ignored

Possible outputs

array: for each line, an associative array whose keys are the headers and values are strings. Underlying method is fgetcsv.

When no line can be read (typically the trailing empty line at the end of the file), no output is produced and the task is skipped for this iteration.

Options

Code Type Required Default Description
file_path string X Path of the file to read from (absolute or relative to the current working directory)
delimiter string ; CSV delimiter
enclosure string " CSV enclosure character
escape string \ CSV escape character
headers array|null null Static list of CSV headers. If null, headers are read from the first line of the file; otherwise the first line is read as data. An empty list throws an UnexpectedValueException
mode string rb File open mode (see fopen mode parameter)
log_empty_lines bool false Log a warning when an empty read ends the file (e.g. the line break at the end of the last line). A blank line in the middle of the file is not skipped: it fails with "Number of columns not matching" (unless the file has a single column); cast to bool

Examples

  • Read a CSV file with a contextualized delimiter
    • the delimiter must be passed on execution: -c delimiter:";"
# Task configuration level
entry:
  task: csv_reader
  options:
    file_path: '%kernel.project_dir%/var/data/sample.csv'
    delimiter: '{{ delimiter }}'
  outputs: [log_line]
  • Read a CSV file without header line
# Task configuration level
entry:
  task: csv_reader
  options:
    file_path: '%kernel.project_dir%/var/data/no_header.csv'
    delimiter: ','
    headers: [sku, name, price]
  outputs: [transform]

Notes

  • Each line must contain exactly as many columns as there are headers, otherwise an UnexpectedValueException is thrown.
  • A UTF-8 BOM is removed from the first header when headers are read from the file. If the first line is blank, an UnexpectedValueException is thrown.
  • csv_file and csv_line are added to the error context of the process.
  • The file is closed once fully read: if the task is executed again (e.g. for a new input), the file is read again from its beginning.
  • See also InputCsvReaderTask to read a file path given as input.