fi.pelam.csv.stream

CsvReader

final class CsvReader extends AbstractIterator[CellOrError]

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)
Source
CsvReader.scala
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.

Linear Supertypes
AbstractIterator[CellOrError], Iterator[CellOrError], TraversableOnce[CellOrError], GenTraversableOnce[CellOrError], AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. CsvReader
  2. AbstractIterator
  3. Iterator
  4. TraversableOnce
  5. GenTraversableOnce
  6. AnyRef
  7. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new CsvReader(input: Reader)

    Alternate constructor for just using default separator which is comma.

  2. new CsvReader(inputString: String, separator: Char = CsvConstants.defaultSeparatorChar)

    Alternate constructor for CsvReader providing string input and optionally specifying separator character.

    Alternate constructor for CsvReader providing string input and optionally specifying separator character.

    This alternate constructor exists mainly to make tests and code examples shorter.

  3. new CsvReader(input: Reader, separator: Char)

    Create a new reader while specifying the separator character.

    Create a new reader while specifying the separator character.

    input

    Input can be string or java.io.Reader. Be mindful of the character set.

    separator

    Optional non-default separator character

Type Members

  1. class GroupedIterator[B >: A] extends AbstractIterator[Seq[B]] with Iterator[Seq[B]]

    Definition Classes
    Iterator

Value Members

  1. final def !=(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Definition Classes
    AnyRef → Any
  3. def ++[B >: CellOrError](that: ⇒ GenTraversableOnce[B]): Iterator[B]

    Definition Classes
    Iterator
  4. def /:[B](z: B)(op: (B, CellOrError) ⇒ B): B

    Definition Classes
    TraversableOnce → GenTraversableOnce
  5. def :\[B](z: B)(op: (CellOrError, B) ⇒ B): B

    Definition Classes
    TraversableOnce → GenTraversableOnce
  6. final def ==(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  7. def addString(b: StringBuilder): StringBuilder

    Definition Classes
    TraversableOnce
  8. def addString(b: StringBuilder, sep: String): StringBuilder

    Definition Classes
    TraversableOnce
  9. def addString(b: StringBuilder, start: String, sep: String, end: String): StringBuilder

    Definition Classes
    TraversableOnce
  10. def aggregate[B](z: ⇒ B)(seqop: (B, CellOrError) ⇒ B, combop: (B, B) ⇒ B): B

    Definition Classes
    TraversableOnce → GenTraversableOnce
  11. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  12. def buffered: BufferedIterator[CellOrError]

    Definition Classes
    Iterator
  13. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  14. def collect[B](pf: PartialFunction[CellOrError, B]): Iterator[B]

    Definition Classes
    Iterator
    Annotations
    @migration
    Migration

    (Changed in version 2.8.0) collect has changed. The previous behavior can be reproduced with toSeq.

  15. def collectFirst[B](pf: PartialFunction[CellOrError, B]): Option[B]

    Definition Classes
    TraversableOnce
  16. def contains(elem: Any): Boolean

    Definition Classes
    Iterator
  17. def copyToArray[B >: CellOrError](xs: Array[B], start: Int, len: Int): Unit

    Definition Classes
    Iterator → TraversableOnce → GenTraversableOnce
  18. def copyToArray[B >: CellOrError](xs: Array[B]): Unit

    Definition Classes
    TraversableOnce → GenTraversableOnce
  19. def copyToArray[B >: CellOrError](xs: Array[B], start: Int): Unit

    Definition Classes
    TraversableOnce → GenTraversableOnce
  20. def copyToBuffer[B >: CellOrError](dest: Buffer[B]): Unit

    Definition Classes
    TraversableOnce
  21. def corresponds[B](that: GenTraversableOnce[B])(p: (CellOrError, B) ⇒ Boolean): Boolean

    Definition Classes
    Iterator
  22. def count(p: (CellOrError) ⇒ Boolean): Int

    Definition Classes
    TraversableOnce → GenTraversableOnce
  23. def drop(n: Int): Iterator[CellOrError]

    Definition Classes
    Iterator
  24. def dropWhile(p: (CellOrError) ⇒ Boolean): Iterator[CellOrError]

    Definition Classes
    Iterator
  25. def duplicate: (Iterator[CellOrError], Iterator[CellOrError])

    Definition Classes
    Iterator
  26. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  27. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  28. def exists(p: (CellOrError) ⇒ Boolean): Boolean

    Definition Classes
    Iterator → TraversableOnce → GenTraversableOnce
  29. def filter(p: (CellOrError) ⇒ Boolean): Iterator[CellOrError]

    Definition Classes
    Iterator
  30. def filterNot(p: (CellOrError) ⇒ Boolean): Iterator[CellOrError]

    Definition Classes
    Iterator
  31. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  32. def find(p: (CellOrError) ⇒ Boolean): Option[CellOrError]

    Definition Classes
    Iterator → TraversableOnce → GenTraversableOnce
  33. def flatMap[B](f: (CellOrError) ⇒ GenTraversableOnce[B]): Iterator[B]

    Definition Classes
    Iterator
  34. def fold[A1 >: CellOrError](z: A1)(op: (A1, A1) ⇒ A1): A1

    Definition Classes
    TraversableOnce → GenTraversableOnce
  35. def foldLeft[B](z: B)(op: (B, CellOrError) ⇒ B): B

    Definition Classes
    TraversableOnce → GenTraversableOnce
  36. def foldRight[B](z: B)(op: (CellOrError, B) ⇒ B): B

    Definition Classes
    TraversableOnce → GenTraversableOnce
  37. def forall(p: (CellOrError) ⇒ Boolean): Boolean

    Definition Classes
    Iterator → TraversableOnce → GenTraversableOnce
  38. def foreach[U](f: (CellOrError) ⇒ U): Unit

    Definition Classes
    Iterator → TraversableOnce → GenTraversableOnce
  39. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  40. def grouped[B >: CellOrError](size: Int): GroupedIterator[B]

    Definition Classes
    Iterator
  41. def hasDefiniteSize: Boolean

    Definition Classes
    Iterator → TraversableOnce → GenTraversableOnce
  42. def hasNext: Boolean

    Definition Classes
    CsvReader → Iterator
  43. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  44. def indexOf[B >: CellOrError](elem: B): Int

    Definition Classes
    Iterator
  45. def indexWhere(p: (CellOrError) ⇒ Boolean): Int

    Definition Classes
    Iterator
  46. def isEmpty: Boolean

    Definition Classes
    Iterator → TraversableOnce → GenTraversableOnce
  47. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  48. def isTraversableAgain: Boolean

    Definition Classes
    Iterator → GenTraversableOnce
  49. def length: Int

    Definition Classes
    Iterator
  50. def map[B](f: (CellOrError) ⇒ B): Iterator[B]

    Definition Classes
    Iterator
  51. def max[B >: CellOrError](implicit cmp: Ordering[B]): CellOrError

    Definition Classes
    TraversableOnce → GenTraversableOnce
  52. def maxBy[B](f: (CellOrError) ⇒ B)(implicit cmp: Ordering[B]): CellOrError

    Definition Classes
    TraversableOnce → GenTraversableOnce
  53. def min[B >: CellOrError](implicit cmp: Ordering[B]): CellOrError

    Definition Classes
    TraversableOnce → GenTraversableOnce
  54. def minBy[B](f: (CellOrError) ⇒ B)(implicit cmp: Ordering[B]): CellOrError

    Definition Classes
    TraversableOnce → GenTraversableOnce
  55. def mkString: String

    Definition Classes
    TraversableOnce → GenTraversableOnce
  56. def mkString(sep: String): String

    Definition Classes
    TraversableOnce → GenTraversableOnce
  57. def mkString(start: String, sep: String, end: String): String

    Definition Classes
    TraversableOnce → GenTraversableOnce
  58. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  59. def next(): CellOrError

    Definition Classes
    CsvReader → Iterator
  60. def nextOption(): Option[CellOrError]

  61. def nonEmpty: Boolean

    Definition Classes
    TraversableOnce → GenTraversableOnce
  62. final def notify(): Unit

    Definition Classes
    AnyRef
  63. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  64. def padTo[A1 >: CellOrError](len: Int, elem: A1): Iterator[A1]

    Definition Classes
    Iterator
  65. def partition(p: (CellOrError) ⇒ Boolean): (Iterator[CellOrError], Iterator[CellOrError])

    Definition Classes
    Iterator
  66. def patch[B >: CellOrError](from: Int, patchElems: Iterator[B], replaced: Int): Iterator[B]

    Definition Classes
    Iterator
  67. def product[B >: CellOrError](implicit num: Numeric[B]): B

    Definition Classes
    TraversableOnce → GenTraversableOnce
  68. def reduce[A1 >: CellOrError](op: (A1, A1) ⇒ A1): A1

    Definition Classes
    TraversableOnce → GenTraversableOnce
  69. def reduceLeft[B >: CellOrError](op: (B, CellOrError) ⇒ B): B

    Definition Classes
    TraversableOnce
  70. def reduceLeftOption[B >: CellOrError](op: (B, CellOrError) ⇒ B): Option[B]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  71. def reduceOption[A1 >: CellOrError](op: (A1, A1) ⇒ A1): Option[A1]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  72. def reduceRight[B >: CellOrError](op: (CellOrError, B) ⇒ B): B

    Definition Classes
    TraversableOnce → GenTraversableOnce
  73. def reduceRightOption[B >: CellOrError](op: (CellOrError, B) ⇒ B): Option[B]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  74. def reversed: List[CellOrError]

    Attributes
    protected[this]
    Definition Classes
    TraversableOnce
  75. def sameElements(that: Iterator[_]): Boolean

    Definition Classes
    Iterator
  76. def scanLeft[B](z: B)(op: (B, CellOrError) ⇒ B): Iterator[B]

    Definition Classes
    Iterator
  77. def scanRight[B](z: B)(op: (CellOrError, B) ⇒ B): Iterator[B]

    Definition Classes
    Iterator
  78. val separator: Char

    Optional non-default separator character

  79. def seq: Iterator[CellOrError]

    Definition Classes
    Iterator → TraversableOnce → GenTraversableOnce
  80. def size: Int

    Definition Classes
    TraversableOnce → GenTraversableOnce
  81. def slice(from: Int, until: Int): Iterator[CellOrError]

    Definition Classes
    Iterator
  82. def sliding[B >: CellOrError](size: Int, step: Int): GroupedIterator[B]

    Definition Classes
    Iterator
  83. def span(p: (CellOrError) ⇒ Boolean): (Iterator[CellOrError], Iterator[CellOrError])

    Definition Classes
    Iterator
  84. def sum[B >: CellOrError](implicit num: Numeric[B]): B

    Definition Classes
    TraversableOnce → GenTraversableOnce
  85. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  86. def take(n: Int): Iterator[CellOrError]

    Definition Classes
    Iterator
  87. def takeWhile(p: (CellOrError) ⇒ Boolean): Iterator[CellOrError]

    Definition Classes
    Iterator
  88. def throwOnError: Iterator[StringCell]

    This method converts this reader into exception based error handling, which may be useful in smaller applications that don't expect errors in input.

    This method converts this reader into exception based error handling, which may be useful in smaller applications that don't expect errors in input.

    A RuntimeException will be thrown when error is encountered. After this the iterator is considered exhausted after the error.

    After being wrapped using this method, the reader produces simple StringCells instead of Scala's Either.

    Note

    Note that the stream will be left open in this situation.

  89. def to[Col[_]](implicit cbf: CanBuildFrom[Nothing, CellOrError, Col[CellOrError]]): Col[CellOrError]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  90. def toArray[B >: CellOrError](implicit arg0: ClassTag[B]): Array[B]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  91. def toBuffer[B >: CellOrError]: Buffer[B]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  92. def toIndexedSeq: IndexedSeq[CellOrError]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  93. def toIterable: Iterable[CellOrError]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  94. def toIterator: Iterator[CellOrError]

    Definition Classes
    Iterator → GenTraversableOnce
  95. def toList: List[CellOrError]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  96. def toMap[T, U](implicit ev: <:<[CellOrError, (T, U)]): Map[T, U]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  97. def toSeq: Seq[CellOrError]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  98. def toSet[B >: CellOrError]: Set[B]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  99. def toStream: Stream[CellOrError]

    Definition Classes
    Iterator → GenTraversableOnce
  100. def toString(): String

    Definition Classes
    Iterator → AnyRef → Any
  101. def toTraversable: Traversable[CellOrError]

    Definition Classes
    Iterator → TraversableOnce → GenTraversableOnce
  102. def toVector: Vector[CellOrError]

    Definition Classes
    TraversableOnce → GenTraversableOnce
  103. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  104. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  105. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  106. def withFilter(p: (CellOrError) ⇒ Boolean): Iterator[CellOrError]

    Definition Classes
    Iterator
  107. def zip[B](that: Iterator[B]): Iterator[(CellOrError, B)]

    Definition Classes
    Iterator
  108. def zipAll[B, A1 >: CellOrError, B1 >: B](that: Iterator[B], thisElem: A1, thatElem: B1): Iterator[(A1, B1)]

    Definition Classes
    Iterator
  109. def zipWithIndex: Iterator[(CellOrError, Int)]

    Definition Classes
    Iterator

Inherited from AbstractIterator[CellOrError]

Inherited from Iterator[CellOrError]

Inherited from TraversableOnce[CellOrError]

Inherited from GenTraversableOnce[CellOrError]

Inherited from AnyRef

Inherited from Any

Ungrouped