flow php

Example: Csv

Topic: Data reading

Description

Read data from a csv file.

function from_csv(
    string|Path $path,
    bool $with_header = true,
    bool $empty_to_null = true,
    ?string $delimiter = null,
    ?string $enclosure = null,
    ?string $escape = null,
    int $characters_read_in_line = 1000,
    ?Schema $schema = null
):
  • with_header - default true, if false, the first row will be treated as data
  • empty_to_null - default false, if true, empty string will be treated as null
  • delimiter - the delimiter of the csv file, when not set, it will be auto detected
  • enclosure - the enclosure of the csv file, when not set, it will be auto detected
  • escape - default \, the escape character of the csv file
  • characters_in_line - default 1000, size of chunk used to read lines from the file
  • schema - the schema of the csv file, when not set, it will be auto detected
composer.json
{
    "name": "flow-php/examples",
    "description": "Flow PHP - Examples",
    "license": "MIT",
    "type": "library",
    "require": {
        "flow-php/etl": "1.x-dev",
        "flow-php/etl-adapter-csv": "1.x-dev"
    }
}
code.php
<?php

declare(strict_types=1);

use function Flow\ETL\Adapter\CSV\from_csv;
use function Flow\ETL\DSL\{data_frame, to_stream};

require __DIR__ . '/vendor/autoload.php';

data_frame()
    ->read(from_csv(
        __DIR__ . '/input/dataset.csv',
        with_header: true,
        empty_to_null: true,
        separator: ',',
        enclosure: '"',
        escape: '\\',
        characters_read_in_line: 1000
    ))
    ->collect()
    ->write(to_stream(__DIR__ . '/output.txt', truncate: false))
    ->run();

Output

+----+--------+------------------+--------+
| id |   name |            email | active |
+----+--------+------------------+--------+
|  1 |   John |   [email protected] |   true |
|  2 |   Paul |   [email protected] |   true |
|  3 | George | [email protected] |  false |
|  4 |  Ringo |   [email protected] |   true |
+----+--------+------------------+--------+
4 rows

Contributors

Join us on GitHub external resource
scroll back to top