fi.pelam.csv

stream

package stream

This package contains the streaming oriented API for processing CSV data.

See the reader and writer classes for more information.

Source
package.scala
See also

CsvWriter

CsvReader

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. stream
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. final class CsvReader extends AbstractIterator[CellOrError]

    This class is part of the lower level API for processing CSV data.

    This class is part of the lower level API for processing CSV data. This is a CSV parser that produces the data through the scala.collection.Iterator trait. The data is read into a sequence of StringCell instances. StringCells can be written back to disk with CsvWriter.

    This class does parsing in streaming fashion ie. you should be able to handle files larger than what can fit in RAM, though this has not been tested yet. The actual parsing is delegated to CsvReaderInternal. This class just implements the Scala Iterator interface on top of CsvReaderInternal.

    Error handling

    If an error is encountered in the input a CsvReaderError will be returned as the Left of Either. After this the iterator is exhausted and hasNext will return false. Note that the input stream will be left open in this situation. If next is called when hasNext is false, a NoSuchElementException will be thrown as usual. See the method throwOnError for a shortcut to enable exception based error handling.

    Code example

    This example code will pick the second column from the csvData and print the toString of each Cell from that column. Note the use of throwOnError to bypass the Either normally returned by the reader.

    import fi.pelam.csv.cell._
    import fi.pelam.csv.stream._
    
    val csvData =
        "apple,0.99,3\n" +
        "orange,1.25,2\n" +
        "banana,0.80,4\n"
    
    val pickedCol = ColKey(1)
    
    for (cell <- new CsvReader(csvData).throwOnError; if cell.colKey == pickedCol) {
      println(cell)
    }
    
    // Running the code above will print the cells from the second column:
    Cell containing '0.99' at Row 1, Column B (1)
    Cell containing '1.25' at Row 2, Column B (1)
    Cell containing '0.80' at Row 3, Column B (1)
    Note

    A note on closing the input stream. The java.io.Reader is closed only if the CSV data exhausted. In the case of an error and in the case of stopping to call the read method before the input ends it is the responsibility of the caller to close the stream.

    See also

    TableReader for a friendlier non streaming API.

  2. final case class CsvReaderError(message: String, at: CellKey) extends Product with Serializable

    CsvReader produces these in various situations when it can't understand the input CSV data.

    CsvReader produces these in various situations when it can't understand the input CSV data.

    message

    is a descriptive message.

    at

    last known coordinates in CSV data (or corresponding spreadsheet).

  3. final class CsvReaderInternal extends AnyRef

    State machine based CSV parser which has single method interface which returns an option of eiher cell or error.

    State machine based CSV parser which has single method interface which returns an option of eiher cell or error.

    See CsvReader for friendlier Scala like interface (specifically Iterator).

  4. final class CsvWriter extends AnyRef

    Opposite of CsvReader.

    Opposite of CsvReader. The API is simple. There are just 3 public methods. One to write a single cell and another to write a TraversableOnce of cells.

    The third method goToNextRow should be called last after the last cell to write the line feed at the end of the file.

    The cells are expected to be in top to bottom left to right order. The sequence of cells can contain holes. Empty cells in CSV data will be emitted for those.

    This class handles quoting, but all other special processing is handled by fi.pelam.csv.cell.Cell.serializedString implementations.

    Note

    This class does not flush or close the output stream. Client must take care of that.

Value Members

  1. object CsvReader

  2. object CsvReaderInternal

  3. object CsvWriter

Inherited from AnyRef

Inherited from Any

Ungrouped