Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2 from mcarolan/pullreq405e576d8

Allow construction of a CSV Reader with none-default separators, quotes,...
  • Loading branch information...
commit 747dd5efa1c3e06aa0a35d41af7831b325ea1ba7 2 parents 5e80bda + 405e576
@tototoshi authored
View
25 src/main/scala/com/github/tototoshi/csv/CSVReader.scala
@@ -16,14 +16,14 @@
package com.github.tototoshi.csv
-import au.com.bytecode.opencsv.{ CSVReader => JCSVReader }
+import au.com.bytecode.opencsv.{CSVReader => JCSVReader}
import java.io._
import scala.collection.JavaConversions._
import java.util.NoSuchElementException
+import au.com.bytecode.opencsv
+import opencsv.CSVParser
-class CSVReader protected (reader: Reader) {
-
- private val underlying: JCSVReader = new JCSVReader(reader)
+class CSVReader protected (private val underlying: JCSVReader) {
def apply[A](f: Iterator[Seq[String]] => A): A = {
try {
@@ -84,9 +84,21 @@ object CSVReader {
def apply(file: File, encoding: String = "UTF-8"): CSVReader = open(file, encoding)
@deprecated("Use #open instead", "0.5.0")
- def apply(reader: Reader): CSVReader = open(reader)
+ def apply(reader: Reader): CSVReader = openFromReader(reader)
+
+ def openFromReader(reader: Reader, separator: Char = CSVParser.DEFAULT_SEPARATOR, quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER, numberOfLinesToSkip: Int = JCSVReader.DEFAULT_SKIP_LINES): CSVReader =
+ new CSVReader(new JCSVReader(reader, separator, quote, numberOfLinesToSkip))
+
+ def openFromFile(file: File, encoding: String = "UTF-8", separator: Char = CSVParser.DEFAULT_SEPARATOR, quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER, numberOfLinesToSkip: Int = JCSVReader.DEFAULT_SKIP_LINES): CSVReader = {
+ val fin = new FileInputStream(file)
+ val reader = new InputStreamReader(fin, encoding)
+ openFromReader(reader, separator, quote, numberOfLinesToSkip)
+ }
- def open(reader: Reader): CSVReader = new CSVReader(reader)
+ def openFromPath(filename: String, encoding: String = "UTF-8", separator: Char = CSVParser.DEFAULT_SEPARATOR, quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER, numberOfLinesToSkip: Int = JCSVReader.DEFAULT_SKIP_LINES) : CSVReader =
+ openFromFile(new File(filename), encoding, separator, quote, numberOfLinesToSkip)
+
+ def open(reader: Reader): CSVReader = openFromReader(reader)
def open(file: File): CSVReader = open(file, "UTF-8")
@@ -99,5 +111,4 @@ object CSVReader {
def open(file: String): CSVReader = open(new File(file), "UTF-8")
def open(file: String, encoding: String): CSVReader = open(new File(file), encoding)
-
}
View
5 src/test/resources/beginning-junk-hash-separated-dollar-quote.csv
@@ -0,0 +1,5 @@
+This
+is
+junk
+$Foo $#$Bar $#$Baz $
+$a$#$b$#$c$
View
2  src/test/resources/hash-separated-dollar-quote.csv
@@ -0,0 +1,2 @@
+$Foo $#$Bar $#$Baz $
+$a$#$b$#$c$
View
2  src/test/resources/hash-separated.csv
@@ -0,0 +1,2 @@
+a#b#c
+d#e#f
View
28 src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala
@@ -48,6 +48,34 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
res.mkString should be ("abcdef")
}
+ it("should be constructed with separators") {
+ var res: List[String] = Nil
+
+ using (CSVReader.openFromPath("src/test/resources/hash-separated.csv", separator = '#')) { reader =>
+ reader foreach { fields =>
+ res = res ++ fields
+ }
+ }
+
+ res.mkString should be ("abcdef")
+ }
+
+ it("should be consutrcted with separators and quotes") {
+ using (CSVReader.openFromPath("src/test/resources/hash-separated-dollar-quote.csv", separator = '#', quote = '$')) { reader => {
+ val map = reader.allWithHeaders()
+ map(0)("Foo ") should be ("a")
+ }
+ }
+ }
+
+ it("should be constructed with separators, quotes, and line skipping") {
+ using (CSVReader.openFromPath("src/test/resources/beginning-junk-hash-separated-dollar-quote.csv", separator = '#', quote = '$', numberOfLinesToSkip = 3)) { reader => {
+ val map = reader.allWithHeaders()
+ map(0)("Foo ") should be ("a")
+ }
+ }
+ }
+
it("read CSV from file") {
var res: List[String] = Nil
using (CSVReader.open(new FileReader("src/test/resources/simple.csv"))) { reader =>
Please sign in to comment.
Something went wrong with that request. Please try again.