• Stars
    star
    683
  • Rank 66,158 (Top 2 %)
  • Language
    Scala
  • License
    Other
  • Created over 12 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CSV Reader/Writer for Scala

scala-csv

build.sbt

libraryDependencies += "com.github.tototoshi" %% "scala-csv" % "1.3.10"

Example

import

scala> import com.github.tototoshi.csv._

Reading example

sample.csv

a,b,c
d,e,f

You can create CSVReader instance with CSVReader#open.

scala> val reader = CSVReader.open(new File("sample.csv"))

Reading all lines

scala> val reader = CSVReader.open(new File("sample.csv"))
reader: com.github.tototoshi.csv.CSVReader = com.github.tototoshi.csv.CSVReader@36d0c6dd

scala> reader.all()
res0: List[List[String]] = List(List(a, b, c), List(d, e, f))

scala> reader.close()

Using iterator

scala> val reader = CSVReader.open("sample.csv")
reader: com.github.tototoshi.csv.CSVReader = com.github.tototoshi.csv.CSVReader@22d568da

scala> val it = reader.iterator
it: Iterator[Seq[String]] = non-empty iterator

scala> it.next
res0: Seq[String] = List(a, b, c)

scala> it.next
res1: Seq[String] = List(d, e, f)

scala> it.next
java.util.NoSuchElementException: next on empty iterator
        at com.github.tototoshi.csv.CSVReader$$anon$1$$anonfun$next$1.apply(CSVReader.scala:55)
        at com.github.tototoshi.csv.CSVReader$$anon$1$$anonfun$next$1.apply(CSVReader.scala:55)
        at scala.Option.getOrElse(Option.scala:108)

scala> reader.close()

Reading all lines as Stream

scala> val reader = CSVReader.open(new File("sample.csv"))
reader: com.github.tototoshi.csv.CSVReader = com.github.tototoshi.csv.CSVReader@7dae76b4

scala> reader.toStream
res7: Stream[List[String]] = Stream(List(a, b, c), ?)

Reading one line at a time

There a two ways available. #foreach and #readNext.

scala> val reader = CSVReader.open(new File("sample.csv"))
reader: com.github.tototoshi.csv.CSVReader = com.github.tototoshi.csv.CSVReader@4720a918

scala> reader.foreach(fields => println(fields))
List(a, b, c)
List(d, e, f)

scala> reader.close()
scala> val reader = CSVReader.open(new File("sample.csv"))
reader: com.github.tototoshi.csv.CSVReader = com.github.tototoshi.csv.CSVReader@4b545701

scala> reader.readNext()
res3: Option[List[String]] = Some(List(a, b, c))

scala> reader.readNext()
res4: Option[List[String]] = Some(List(d, e, f))

scala> reader.readNext()
res5: Option[List[String]] = None

scala> reader.close()

Reading a csv file with column headers

with-headers.csv

Foo,Bar,Baz
a,b,c
d,e,f
scala> val reader = CSVReader.open(new File("with-headers.csv"))
reader: com.github.tototoshi.csv.CSVReader = com.github.tototoshi.csv.CSVReader@1a64e307

scala> reader.allWithHeaders()
res0: List[Map[String,String]] = List(Map(Foo -> a, Bar -> b, Baz -> c), Map(Foo -> d, Bar -> e, Baz -> f))

Writing example

Writing all lines with #writeAll

scala> val f = new File("out.csv")

scala> val writer = CSVWriter.open(f)
writer: com.github.tototoshi.csv.CSVWriter = com.github.tototoshi.csv.CSVWriter@783f77f1

scala> writer.writeAll(List(List("a", "b", "c"), List("d", "e", "f")))

scala> writer.close()

Writing one line at a time with #writeRow

scala> val f = new File("out.csv")

scala> val writer = CSVWriter.open(f)
writer: com.github.tototoshi.csv.CSVWriter = com.github.tototoshi.csv.CSVWriter@41ad4de1

scala> writer.writeRow(List("a", "b", "c"))

scala> writer.writeRow(List("d", "e", "f"))

scala> writer.close()

Appending lines to the file that already exists

The default behavior of CSVWriter#open is overwriting. To append lines to the file that already exists, Set the append flag true.

scala> val writer = CSVWriter.open("a.csv", append = true)
writer: com.github.tototoshi.csv.CSVWriter = com.github.tototoshi.csv.CSVWriter@67a84246

scala> writer.writeRow(List("4", "5", "6"))

scala> writer.close()

Customizing the format

CSVReader/Writer#open takes CSVFormat implicitly. Define your own CSVFormat when you want to change the CSV's format.

scala> :paste
// Entering paste mode (ctrl-D to finish)

implicit object MyFormat extends DefaultCSVFormat {
  override val delimiter = '#'
}
val w = CSVWriter.open(new java.io.OutputStreamWriter(System.out))

// 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"

Changing the encoding

By default the UTF-8 is set. To change it, for example, to ISO-8859-1 you can set it in the CSVReader:

scala> val reader = CSVReader.open(filepath, "ISO-8859-1")
reader: com.github.tototoshi.csv.CSVReader = com.github.tototoshi.csv.CSVReader@6bcb69ba

Dev

$ git clone https://github.com/tototoshi/scala-csv.git
$ cd scala-csv
$ sbt
> test

License

Apache 2.0

More Repositories

1

slick-joda-mapper

Slick with JodaTime
Scala
246
star
2

play-flyway

Flyway plugin for Play >= 2.1
Scala
87
star
3

sbt-slick-codegen

slick-codegen compile hook for sbt
Shell
69
star
4

sbt-musical

NO MUSIC, NO BUILD. Enjoy your compile time.
Scala
63
star
5

play-json4s

Scala
52
star
6

play-json-naming

Custom naming convention for play-json to map snake_case json to camelCase case classes
Scala
50
star
7

staticmock

A mockery-like DSL to replace static method in test.
PHP
42
star
8

sbt-build-files-watcher

show message when sbt build files changed
Scala
27
star
9

play-ascii-art-plugin

play-ascii-art-plugin
Scala
23
star
10

scala-http-client

A thin wrapper of Apache HttpClient
Scala
20
star
11

play-joda-routes-binder

QueryString / Path Binder for Play 2.x
Scala
18
star
12

scala-js-slide

slide show written in Scala.js
Scala
17
star
13

play-json-generic

Scala
14
star
14

scala-fixture

Simple fixture library for Scala
Scala
14
star
15

sbt-slick-codegen-example

Scala
14
star
16

alpaca

A tiny scripting language for web browser automation
Scala
12
star
17

play-reactjs-example

Simple Twitter App with Play2 and React.js
Scala
12
star
18

scala-base64

BASE64ใ‚จใƒณใ‚ณใƒผใƒ€
Scala
10
star
19

scala-base62

Base62 encode/decoder for Scala
Scala
10
star
20

dbcache

Cache library with RDB backend
Scala
9
star
21

lift-json-play-module

lift-json module for Play20
Scala
9
star
22

sbt-install

Install script for sbt
Shell
9
star
23

gfm-editor

Github Flavored Markdown Editor
CoffeeScript
9
star
24

play-dependency-graph

9
star
25

gitter-twitter-bot

Gitter twitter bot
Scala
7
star
26

mvnsearch

Scala
7
star
27

moomin-el

Edit MoinMoin with Emacs
Emacs Lisp
6
star
28

dotemacs

Emacs Lisp
6
star
29

gistub-el

Gistub client for Emacs
Emacs Lisp
6
star
30

nyanda

Database Accessor for cats
Scala
6
star
31

selector

jQuery-like selector
Scala
6
star
32

play-auth-social

An experimental implementation of social login with play-auth
Scala
6
star
33

configpath

typosafe typesafe config
Scala
5
star
34

feedly-tweet

feedly โ†’ twitter
Python
5
star
35

sbt-automkcol

Fork of webdav4sbt(https://bitbucket.org/diversit/webdav4sbt/src)
Scala
5
star
36

unfiltered-scalate-scalaquery-example-bookmarks

JavaScript
5
star
37

jenkins-idobata-notifier-plugin

Idobata Notifier for Jenkins
Java
5
star
38

sbt-shintyoku-doudesuka

้€ฒๆ—ใฉใ†ใงใ™ใ‹๏ผŸ
Scala
5
star
39

scala-itunes

Scala
5
star
40

php-parser-combinator

Parse combinator for PHP
PHP
5
star
41

play-scalate

This project has been migrated to http://github.com/scalate/play-scalate.
Scala
5
star
42

kushi

A very simple HTTP proxy written in Scala and Finagle.
Scala
4
star
43

OAuthSample

OAuthใฎใ‚ตใƒณใƒ—ใƒซใงใ™
Java
4
star
44

hatenacala

ใฏใฆใชใƒ€ใ‚คใ‚ขใƒชใƒผ็”จใฎใ‚ณใƒžใƒณใƒ‰ใƒฉใ‚คใƒณใƒ„ใƒผใƒซ
Scala
4
star
45

seedbed

An alternative to using database fixtures in your Scala unit tests
Scala
4
star
46

helm-find-files-in-project

Emacs Lisp
4
star
47

sbt-hot-reload-example

This project shows how to implement Hot Reload by sbt like Play framework.
Scala
4
star
48

github-timeline-tweet

Github timeline โ†’ twitter
Python
4
star
49

tostring-macro

Scala
3
star
50

play-ws-standalone-json4s

Scala
3
star
51

scalikejdbc-batch.g8

g8 template for scalikejdbc
Scala
3
star
52

stumpwmrc

3
star
53

tototoshi.github.io

JavaScript
3
star
54

json2tsv

convert json log to tsv
Scala
3
star
55

epub4s

Scala
3
star
56

scala-tsv

tsvใƒ•ใ‚กใ‚คใƒซ็”จใฎใ‚†ใƒผใฆใƒใ‚Šใฆใƒ
Scala
3
star
57

cl-misc

common lisp
Common Lisp
2
star
58

ansible-playbook-moinmoin

CSS
2
star
59

dirclean

remove unix backup files
C
2
star
60

gitlab-merge-request

Shell
2
star
61

scalatra-bootstrap.g8

JavaScript
2
star
62

play-twitter-oauth-sample

Sample application of twitter oauth with Play 2.3
Scala
2
star
63

tt-el

emacs lisp utilities especially for me
Emacs Lisp
2
star
64

play-twitterauth

Twitter Login for Play2
Scala
2
star
65

rfc-view-el

RFC viewer for emacs
Emacs Lisp
2
star
66

hcut

Cutter for text files with header
Python
2
star
67

g8.g8

Scala
2
star
68

org-exporter-for-scala

JavaScript
2
star
69

screenrc

2
star
70

firefox-addon-for-twitter

Post the current page to Twitter
JavaScript
2
star
71

my-xmonad.hs

Haskell
2
star
72

unfiltered-example-bookmarks

An example app for Unfiltered, a Scala HTTP/REST toolkit
Scala
2
star
73

queriform

SQL formatter
Scala
2
star
74

slides

JavaScript
2
star
75

anything-find-files-in-project

This project is deprecated and moved to tototoshi/helm-find-files-in-project
Emacs Lisp
2
star
76

dotfiles

dotfiles
Emacs Lisp
2
star
77

play-install

Install script for Play Framework 2.0
Shell
2
star
78

firefox-addon-for-eijiro-on-the-web

่‹ฑ่พž้ƒŽ on the web ็”จใฎFirefoxใ‚ขใƒ‰ใ‚ชใƒณ
JavaScript
2
star
79

moomin

Command line tool for MoinMoin wiki.
Python
1
star
80

php-install

My PHP installer for PHP 5.3-5.6
Shell
1
star
81

php-slack-webhook

PHP
1
star
82

play-reverse-router-extension

Scala
1
star
83

git-grep-el

git grep in emacs
Emacs Lisp
1
star
84

hello-play-26.g8

g8 template for play 2.6
Scala
1
star
85

php-slim-project-template

Template for small Slim Framework projects
HTML
1
star
86

python-install

My Python installer
Shell
1
star
87

lgtmify

LGTM stamp
PHP
1
star
88

freearg

Scala
1
star
89

date_range

Python
1
star
90

memo-el

My instant memo
Emacs Lisp
1
star
91

hello.g8

Scala
1
star
92

csv2xls

Command line utility to convert csv to horrible format
Scala
1
star
93

simple-slides

Simple presentation tool
JavaScript
1
star
94

homebrew-butter

Ruby
1
star
95

scala-ojisan

1
star
96

phpunithelper

Yet another PHPUnit skeleton generator.
PHP
1
star
97

markdown-to-pdf-example

Makefile
1
star
98

phpmethodreplacer

Replace static method at runtime
PHP
1
star
99

sawk

awk in scala?
Scala
1
star
100

elasticsearch-embedded-test-example

Scala
1
star