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.
- Service:
CleverAge\ProcessBundle\Task\File\Csv\CsvReaderTask - Code:
csv_reader(task: csv_reader) - Iterable task
Input is ignored
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.
| 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 |
- Read a CSV file with a contextualized delimiter
- the delimiter must be passed on execution:
-c delimiter:";"
- the delimiter must be passed on execution:
# 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]- Each line must contain exactly as many columns as there are headers, otherwise an
UnexpectedValueExceptionis 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
UnexpectedValueExceptionis thrown. csv_fileandcsv_lineare 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.