Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 3 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
45 README.md
@@ -16,7 +16,7 @@ libraryDependencies += "com.github.tototoshi" %% "scala-csv" % "0.7.1"
### import
```scala
-scala> import com.github.tototoshi.csv.CSVReader
+scala> import com.github.tototoshi.csv._
```
### Reading example
@@ -124,25 +124,8 @@ scala> reader.allWithHeaders()
res0: List[Map[String,String]] = List(Map(Foo -> a, Bar -> b, Baz -> c), Map(Foo -> d, Bar -> e, Baz -> f))
````
-#### Using loan pattern
-
-```scala
-scala> CSVReader.open("src/test/resources/simple.csv") { lines =>
- | lines.foreach(println)
- | }
-List(a, b, c)
-List(d, e, f)
-```
-
### Writing example
-#### import
-```scala
-scala> import com.github.tototoshi.csv.CSVWriter
-import com.github.tototoshi.csv.CSVWriter
-
-```
-
#### Writing all lines with #writeAll
```scala
@@ -184,17 +167,29 @@ scala> writer.writeRow(List("4", "5", "6"))
scala> writer.close()
```
-#### Using loan pattern
+### Customizing the format
```scala
-scala> CSVWriter.open("a.csv") { writer =>
- | writer.writeRow(List(1, 2, 3))
- | }
+scala> :paste
+// Entering paste mode (ctrl-D to finish)
+
+implicit object MyFormat extends DefaultCSVFormat {
+ override val separator = '#'
+ override val quoteChar = ''
+}
+val w = CSVWriter.open(new java.io.OutputStreamWriter(System.out))
-scala> io.Source.fromFile("a.csv").mkString
-res0: String =
-""1","2","3"
+// Exiting paste mode, now interpreting.
+
+defined module MyFormat
+w: com.github.tototoshi.csv.CSVWriter = com.github.tototoshi.csv.CSVWriter@6cd66afa
+
+scala> w.writeRow(List(1, 2, 3))
+"1"#"2"#"3"
```
+CSVReader/Writer#open takes CSVFormat implicitly.
+Define your own CSVFormat when you want to change the CSV's format.
+
## License
[Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0)
View
5 src/main/scala/com/github/tototoshi/csv/CSVFormat.scala
@@ -20,9 +20,9 @@ trait CSVFormat {
val separator: Char
- val quote: Char
+ val quoteChar: Char
- val escape: Char
+ val escapeChar: Char
val numberOfLinesToSkip: Int
@@ -30,4 +30,5 @@ trait CSVFormat {
val ignoreLeadingWhiteSpace: Boolean
+ val lineEnd: String
}
View
6 src/main/scala/com/github/tototoshi/csv/CSVReader.scala
@@ -24,7 +24,7 @@ import au.com.bytecode.opencsv
class CSVReader protected (private val underlying: JCSVReader) {
- @deprecated("0.8.0")
+ @deprecated("No longer supported", "0.8.0")
def apply[A](f: Iterator[Seq[String]] => A): A = {
try {
f(this.iterator)
@@ -83,13 +83,13 @@ object CSVReader {
val DEFAULT_ENCODING = "UTF-8"
@deprecated("Use #open instead", "0.5.0")
- def apply(file: File, encoding: String = "UTF-8"): CSVReader = open(file, encoding)
+ def apply(file: File, encoding: String = "UTF-8"): CSVReader = open(file, encoding)(defaultCSVFormat)
@deprecated("Use #open instead", "0.5.0")
def apply(reader: Reader): CSVReader = open(reader)(defaultCSVFormat)
def open(reader: Reader)(implicit format: CSVFormat): CSVReader =
- new CSVReader(new JCSVReader(reader, format.separator, format.quote, format.numberOfLinesToSkip))
+ new CSVReader(new JCSVReader(reader, format.separator, format.quoteChar, format.numberOfLinesToSkip))
def open(file: File)(implicit format: CSVFormat): CSVReader = {
open(file, this.DEFAULT_ENCODING)(format)
View
35 src/main/scala/com/github/tototoshi/csv/CSVWriter.scala
@@ -20,10 +20,9 @@ import scala.collection.JavaConversions._
import au.com.bytecode.opencsv.{ CSVWriter => JCSVWriter }
import java.io.{ File, Writer, FileOutputStream, OutputStreamWriter }
-class CSVWriter protected (writer: Writer) {
-
- private val underlying: JCSVWriter = new JCSVWriter(writer)
+class CSVWriter protected (protected val underlying: JCSVWriter) {
+ @deprecated("No longer supported","0.8.0")
def apply[A](f: CSVWriter => A): A = {
try {
f(this)
@@ -54,30 +53,36 @@ class CSVWriter protected (writer: Writer) {
object CSVWriter {
@deprecated("Use #open instead", "0.5.0")
- def apply(file: File, encoding: String = "UTF-8"): CSVWriter = open(file, false, encoding)
+ def apply(file: File, encoding: String = "UTF-8")(implicit format: CSVFormat): CSVWriter =
+ open(file, false, encoding)(defaultCSVFormat)
@deprecated("Use #open instead", "0.5.0")
- def apply(writer: Writer): CSVWriter = open(writer)
+ def apply(writer: Writer): CSVWriter = open(writer)(defaultCSVFormat)
+
+ def open(writer: Writer)(implicit format: CSVFormat): CSVWriter = {
+ val jcsvWriter = new JCSVWriter(writer, format.separator, format.quoteChar, format.escapeChar, format.lineEnd)
+ new CSVWriter(jcsvWriter)
+ }
- def open(writer: Writer): CSVWriter = new CSVWriter(writer)
- def open(file: File): CSVWriter = open(file, false, "UTF-8")
+ def open(file: File)(implicit format: CSVFormat): CSVWriter = open(file, false, "UTF-8")(format)
- def open(file: File, encoding: String): CSVWriter = open(file, false, encoding)
+ def open(file: File, encoding: String)(implicit format: CSVFormat): CSVWriter = open(file, false, encoding)(format)
- def open(file: File, append: Boolean): CSVWriter = open(file, append, "UTF-8")
+ def open(file: File, append: Boolean)(implicit format: CSVFormat): CSVWriter = open(file, append, "UTF-8")(format)
- def open(file: File, append: Boolean, encoding: String): CSVWriter = {
+ def open(file: File, append: Boolean, encoding: String)(implicit format: CSVFormat): CSVWriter = {
val fos = new FileOutputStream(file, append)
val writer = new OutputStreamWriter(fos, encoding)
- open(writer)
+ open(writer)(format)
}
- def open(file: String): CSVWriter = open(file, false, "UTF-8")
+ def open(file: String)(implicit format: CSVFormat): CSVWriter = open(file, false, "UTF-8")(format)
- def open(file: String, encoding: String): CSVWriter = open(file, false, encoding)
+ def open(file: String, encoding: String)(implicit format: CSVFormat): CSVWriter = open(file, false, encoding)(format)
- def open(file: String, append: Boolean): CSVWriter = open(file, append, "UTF-8")
+ def open(file: String, append: Boolean)(implicit format: CSVFormat): CSVWriter = open(file, append, "UTF-8")(format)
- def open(file: String, append: Boolean, encoding: String): CSVWriter = open(new File(file), append, encoding)
+ def open(file: String, append: Boolean, encoding: String)(implicit format: CSVFormat): CSVWriter =
+ open(new File(file), append, encoding)(format)
}
View
8 src/main/scala/com/github/tototoshi/csv/DefaultCSVFormat.scala
@@ -15,15 +15,15 @@
*/
package com.github.tototoshi.csv
-import au.com.bytecode.opencsv.{ CSVParser, CSVReader => JCSVReader }
+import au.com.bytecode.opencsv.{ CSVParser, CSVReader => JCSVReader, CSVWriter => JCSVWriter }
trait DefaultCSVFormat extends CSVFormat {
val separator: Char = CSVParser.DEFAULT_SEPARATOR
- val quote: Char = CSVParser.DEFAULT_QUOTE_CHARACTER
+ val quoteChar: Char = CSVParser.DEFAULT_QUOTE_CHARACTER
- val escape: Char = CSVParser.DEFAULT_ESCAPE_CHARACTER
+ val escapeChar: Char = CSVParser.DEFAULT_ESCAPE_CHARACTER
val numberOfLinesToSkip: Int = JCSVReader.DEFAULT_SKIP_LINES
@@ -31,4 +31,6 @@ trait DefaultCSVFormat extends CSVFormat {
val ignoreLeadingWhiteSpace: Boolean = CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE
+ val lineEnd: String = JCSVWriter.DEFAULT_LINE_END
+
}
View
4 src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala
@@ -63,7 +63,7 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
it("should be consutrcted with separators and quotes") {
implicit object format extends DefaultCSVFormat {
override val separator: Char = '#'
- override val quote: Char = '$'
+ override val quoteChar: Char = '$'
}
using (CSVReader.open("src/test/resources/hash-separated-dollar-quote.csv")) { reader => {
@@ -76,7 +76,7 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
it("should be constructed with separators, quotes, and line skipping") {
implicit object format extends DefaultCSVFormat {
override val separator: Char = '#'
- override val quote: Char = '$'
+ override val quoteChar: Char = '$'
override val numberOfLinesToSkip: Int = 3
}
using (CSVReader.open("src/test/resources/beginning-junk-hash-separated-dollar-quote.csv")) { reader => {
View
7 src/test/scala/com/github/tototoshi/csv/CSVWriterSpec.scala
@@ -22,13 +22,6 @@ class CSVWriterSpec extends FunSpec with ShouldMatchers with BeforeAndAfter with
describe("CSVWriter") {
describe ("#apply") {
- it ("should provide loan pattern") {
- CSVWriter.open("test.csv") { writer =>
- writer.writeRow(List(1, 2, 3))
- writer.writeRow(List(4, 5, 6))
- }
- readFileAsString("test.csv") should be ("\"1\",\"2\",\"3\"\n\"4\",\"5\",\"6\"\n")
- }
it ("should close csv writer") {
val writer = CSVWriter.open("test.csv")
writer(w => ())

No commit comments for this range

Something went wrong with that request. Please try again.