Skip to content

Commit 405e576

Browse files
author
Martin Carolan
committed
Allow construction of a CSV Reader with none-default separators, quotes, 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
1 parent 770578b commit 405e576

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

src/main/scala/com/github/tototoshi/csv/CSVReader.scala

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616

1717
package com.github.tototoshi.csv
1818

19-
import au.com.bytecode.opencsv.{ CSVReader => JCSVReader }
19+
import au.com.bytecode.opencsv.{CSVReader => JCSVReader}
2020
import java.io._
2121
import scala.collection.JavaConversions._
2222
import java.util.NoSuchElementException
23+
import au.com.bytecode.opencsv
24+
import opencsv.CSVParser
2325

24-
class CSVReader protected (reader: Reader) {
25-
26-
private val underlying: JCSVReader = new JCSVReader(reader)
26+
class CSVReader protected (private val underlying: JCSVReader) {
2727

2828
def apply[A](f: Iterator[Seq[String]] => A): A = {
2929
try {
@@ -84,9 +84,21 @@ object CSVReader {
8484
def apply(file: File, encoding: String = "UTF-8"): CSVReader = open(file, encoding)
8585

8686
@deprecated("Use #open instead", "0.5.0")
87-
def apply(reader: Reader): CSVReader = open(reader)
87+
def apply(reader: Reader): CSVReader = openFromReader(reader)
88+
89+
def openFromReader(reader: Reader, separator: Char = CSVParser.DEFAULT_SEPARATOR, quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER, numberOfLinesToSkip: Int = JCSVReader.DEFAULT_SKIP_LINES): CSVReader =
90+
new CSVReader(new JCSVReader(reader, separator, quote, numberOfLinesToSkip))
91+
92+
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 = {
93+
val fin = new FileInputStream(file)
94+
val reader = new InputStreamReader(fin, encoding)
95+
openFromReader(reader, separator, quote, numberOfLinesToSkip)
96+
}
8897

89-
def open(reader: Reader): CSVReader = new CSVReader(reader)
98+
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 =
99+
openFromFile(new File(filename), encoding, separator, quote, numberOfLinesToSkip)
100+
101+
def open(reader: Reader): CSVReader = openFromReader(reader)
90102

91103
def open(file: File): CSVReader = open(file, "UTF-8")
92104

@@ -99,5 +111,4 @@ object CSVReader {
99111
def open(file: String): CSVReader = open(new File(file), "UTF-8")
100112

101113
def open(file: String, encoding: String): CSVReader = open(new File(file), encoding)
102-
103114
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This
2+
is
3+
junk
4+
$Foo $#$Bar $#$Baz $
5+
$a$#$b$#$c$
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$Foo $#$Bar $#$Baz $
2+
$a$#$b$#$c$

src/test/resources/hash-separated.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a#b#c
2+
d#e#f

src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,34 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
4848
res.mkString should be ("abcdef")
4949
}
5050

51+
it("should be constructed with separators") {
52+
var res: List[String] = Nil
53+
54+
using (CSVReader.openFromPath("src/test/resources/hash-separated.csv", separator = '#')) { reader =>
55+
reader foreach { fields =>
56+
res = res ++ fields
57+
}
58+
}
59+
60+
res.mkString should be ("abcdef")
61+
}
62+
63+
it("should be consutrcted with separators and quotes") {
64+
using (CSVReader.openFromPath("src/test/resources/hash-separated-dollar-quote.csv", separator = '#', quote = '$')) { reader => {
65+
val map = reader.allWithHeaders()
66+
map(0)("Foo ") should be ("a")
67+
}
68+
}
69+
}
70+
71+
it("should be constructed with separators, quotes, and line skipping") {
72+
using (CSVReader.openFromPath("src/test/resources/beginning-junk-hash-separated-dollar-quote.csv", separator = '#', quote = '$', numberOfLinesToSkip = 3)) { reader => {
73+
val map = reader.allWithHeaders()
74+
map(0)("Foo ") should be ("a")
75+
}
76+
}
77+
}
78+
5179
it("read CSV from file") {
5280
var res: List[String] = Nil
5381
using (CSVReader.open(new FileReader("src/test/resources/simple.csv"))) { reader =>

0 commit comments

Comments
 (0)