Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow construction of a CSV Reader with none-default separators, quotes,... #2

Merged
merged 1 commit into from

2 participants

@mcarolan

... or number of lines to skip before reading.

Added these as new construction methods with defaulted parameters. Could not add the defaulted parameters to original open methods as defaulted can only be given to methods with no additional overloads

Martin Carolan Allow construction of a CSV Reader with none-default separators, quot…
…es, or number of lines to skip before reading.

Added these as new construction methods with defaulted parameters. Could not add the defaulted parameters to original open methods as defaulted  can only be given to methods with no additional overloads
405e576
@tototoshi
Owner

Thanks, I will merge this.
I'm thinking about renaming #openFromFile to #fromFile to follow the naming convention of Scala standard library.

@tototoshi tototoshi merged commit 747dd5e into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 23, 2013
  1. Allow construction of a CSV Reader with none-default separators, quot…

    Martin Carolan authored
    …es, or number of lines to skip before reading.
    
    Added these as new construction methods with defaulted parameters. Could not add the defaulted parameters to original open methods as defaulted  can only be given to methods with no additional overloads
This page is out of date. Refresh to see the latest.
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 =>
Something went wrong with that request. Please try again.