CSV functions
On this page
The native CSV reader, which requires the extension. See Reading CSV files for options, types, missing values, and errors.
Reading a file
read_csv()
Reads a delimited text file into typed columns, with the records, names, guessed types, values, and problem messages of phibble’s PHP reader and readr::read_delim().
- Dates are int days since 1970-01-01, and datetimes are int microseconds since the epoch, in UTC.
NAvalues arenull. failedcounts values that did not parse under the column’s type; they arenull. Skipped columns arenull.
Phit\read_csv( string $path, array $options = [],): array| Parameter | Type | Default | Description |
|---|---|---|---|
$path |
string |
required | A readable file (bytes are passed through; no encoding validation) |
$options |
array |
[] |
readr’s defaults when omitted |
Returns array{names: list<string>, columns: list<null\|array{type: 'bool'\|'int'\|'float'\|'string'\|'date'\|'datetime', values: list<bool\|int\|float\|string\|null>, failed: int}>, problems: list<string>}
Errors ValueError: a missing or unreadable file, or an invalid option.
See also Details
Reading in chunks
CsvReader
Reads a CSV file a chunk of rows at a time, as phibble’s DataFrame::csvChunks() (after readr::read_delim_chunked()): only the current chunk is held. Options are read_csv()’s; types not given are guessed from the first chunk (guess_max of its rows) and every later chunk is parsed as those types. Problem rows are numbered from the start of the file.
CsvReader::__construct()
Opens the file and reads its header line.
public function __construct(string $path, array $options = [])| Parameter | Description |
|---|---|
$path |
A readable file |
$options |
Errors ValueError: missing or unreadable file, or an invalid option.
CsvReader::names()
Column names: the header’s, or X1..Xn with col_names false; [] for an empty file.
public function names(): arrayReturns list<string>
CsvReader::next()
The next chunk of at most $rows rows in read_csv()’s shape, with the 1-based position of its first row and its row count; null once no rows remain. A chunk without rows comes only to carry problems found after the last row.
public function next(int $rows): ?arrayReturns null\|array{position: int, rows: int, names: list<string>, columns: list<null\|array{type: 'bool'\|'int'\|'float'\|'string'\|'date'\|'datetime', values: list<bool\|int\|float\|string\|null>, failed: int}>, problems: list<string>}
Errors ValueError: rows below 1, or a read error.