Working with CSV files
Reading CSVs
FEAST has a built-in CSV reader class class (\Feast\Csv\CsvReader
). This class allows working with CSV data as named
Key arrays instead of numeric keys, reducing errors.
The CSV Reader class constructor takes the same parameters as fopen
. You can find more
info here.
The CSV Reader has several useful methods for working with data.
-
setHeaderRow
- This method takes a row number (starting at 0) and an optional additional headers parameter. The additional headers parameter is useful if the CSV has a header row and then some additional data that is not actually part of the dataset. -
getHeader
- This method returns the header row info in numeric keyed array format. -
setCsvOptions
- This method takes a separator, enclosure, and escape character. All parameters are defaulted, so named parameters can be used. -
getFileHandler
- This method retrieves the underlyingSplFileObject
. -
getIterator
- This method yields the next row and can be used in a foreach loop to save memory. -
getAll
- This method returns all the rows from the CSV. This method will use large amounts of memory on large files. -
getNextLine
- This method returns the next row from the CSV. -
rewind
- This method rewinds the CSV to the beginning of the CSV (or after all headers, if headers are set).
Writing to CSVs
FEAST contains a CSV writer class (\Feast\Csv\CsvWriter
) that operates similarly to the reader class.
The CSV Writer class constructor takes the same parameters as fopen
. You can find more
info here.
The CSV Reader has several useful methods for working with data.
-
setHeaderRow
- This method takes a row number (starting at 0) and an optional additional headers parameter. The additional headers parameter is useful if the CSV has a header row and then some additional data that is not actually part of the dataset. -
setHeader
- This method takes an array of column headers to be used. -
setCsvOptions
- This method takes a separator, enclosure, and escape character. All parameters are defaulted, so named parameters can be used. -
getFileHandler
- This method retrieves the underlyingSplFileObject
. -
writeHeader
- This method writes the header to the file. If not called beforewriteLine
, it will be called automatically. -
writeLine
- This method writes a line to the CSV. Named keys are matched up to the header. If header is not set, then the array is written as is.
Powered by FEAST Framework
See this project at https://github.com/feastframework/documentation