All Projects → zamblauskas → scala-csv-parser

zamblauskas / scala-csv-parser

Licence: MIT License
CSV parser library.

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to scala-csv-parser

Csv
CSV Decoding and Encoding for Elixir
Stars: ✭ 398 (+1558.33%)
Mutual labels:  csv, parsing
Readr
Read flat files (csv, tsv, fwf) into R
Stars: ✭ 821 (+3320.83%)
Mutual labels:  csv, parsing
Semantic Csv
Higher level tools for working with CSV data and files
Stars: ✭ 232 (+866.67%)
Mutual labels:  csv, parsing
klar-EDA
A python library for automated exploratory data analysis
Stars: ✭ 15 (-37.5%)
Mutual labels:  csv
pcap-processor
Read and process pcap files using this nifty tool
Stars: ✭ 36 (+50%)
Mutual labels:  csv
elm-csv
Decode CSV in the most boring way possible.
Stars: ✭ 23 (-4.17%)
Mutual labels:  csv
python-yamlable
A thin wrapper of PyYaml to convert Python objects to YAML and back
Stars: ✭ 28 (+16.67%)
Mutual labels:  parsing
simplifai
Free automated deep learning for spreadsheets
Stars: ✭ 17 (-29.17%)
Mutual labels:  csv
xcfg
X (weighted / probabilistic) Context-Free Grammars
Stars: ✭ 17 (-29.17%)
Mutual labels:  parsing
Jekyll
Call of Duty XAsset exporter that dumps raw assets from a game's memory.
Stars: ✭ 29 (+20.83%)
Mutual labels:  csv
hxjsonast
Parse JSON into position-aware AST with Haxe!
Stars: ✭ 28 (+16.67%)
Mutual labels:  parsing
libcitygml
C++ Library for CityGML Parsing and Visualization
Stars: ✭ 69 (+187.5%)
Mutual labels:  parsing
org-clock-csv
Export Emacs org-mode clock entries to CSV format.
Stars: ✭ 80 (+233.33%)
Mutual labels:  csv
datamaker
Data generator command-line tool and library. Create JSON, CSV, XML data from templates.
Stars: ✭ 23 (-4.17%)
Mutual labels:  csv
m3u8
Parse and generate m3u8 playlists for Apple HTTP Live Streaming (HLS) in Ruby.
Stars: ✭ 96 (+300%)
Mutual labels:  parsing
MimeParser
Mime parsing in Swift | Relevant RFCs: RFC 822, RFC 2045, RFC 2046
Stars: ✭ 18 (-25%)
Mutual labels:  parsing
tableschema-go
A Go library for working with Table Schema.
Stars: ✭ 41 (+70.83%)
Mutual labels:  csv
desktop
Extendable calculator for the 21st Century ⚡
Stars: ✭ 85 (+254.17%)
Mutual labels:  parsing
django-excel-response
Django package to easily render Excel spreadsheets
Stars: ✭ 74 (+208.33%)
Mutual labels:  csv
GitHub-WebHook
🐱 Validates and processes GitHub's webhooks
Stars: ✭ 25 (+4.17%)
Mutual labels:  parsing

Github Action Maven Central Codacy Badge Codacy Badge

About

CSV parser library for Scala. Easiest way to convert CSV string representation into a case class.

Usage

import zamblauskas.csv.parser._

case class Person(name: String, age: Int, city: Option[String])

val csv = """
            |name,age,height,city
            |Emily,33,169,London
            |Thomas,25,,
          """.stripMargin

val result = Parser.parse[Person](csv)

result shouldBe Right(List(Person("Emily",33,Some("London")), Person("Thomas",25,None)))

ColumnReads[T]

Example above used a macro generated ColumnReads[Person]. You can define one manually if the generated one does not fit your use case (e.g. column names differ from case class parameter names).

This is identical to what the macro generates for a Person case class:

import zamblauskas.csv.parser._
import zamblauskas.functional._

case class Person(name: String, age: Int, city: Option[String])

implicit val personReads: ColumnReads[Person] = (
  column("name").as[String]    and
  column("age").as[Int]        and
  column("city").asOpt[String]
)(Person)

val csv = """
            |name,age,height,city
            |Emily,33,169,London
            |Thomas,25,,
          """.stripMargin

val result = Parser.parse[Person](csv)

result shouldBe Right(List(Person("Emily",33,Some("London")), Person("Thomas",25,None)))

Alternative column names

If columns have two or more alternative names (e.g. in different languages), you can use an or combinator.

import zamblauskas.csv.parser._
import zamblauskas.functional._
import Parser.parse

case class Person(age: Int, city: String)

implicit val personReads: ColumnReads[Person] = (
  (column("age").as[Int] or column("alter").as[Int]) and
  (column("city").as[String] or column("stadt").as[String])
)(Person)

val englishCsv =
  """
    |age,city
    |33,London
  """.stripMargin

val germanCsv =
  """
    |alter,stadt
    |33,London
  """.stripMargin

parse[Person](englishCsv) shouldBe parse[Person](germanCsv)

Alternative reads

Example above can be rewritten to use alternative ColumnReads instead of alternative column names.

import zamblauskas.csv.parser._
import zamblauskas.functional._
import Parser.parse

case class Person(age: Int, city: String)

val englishPersonReads: ColumnReads[Person] = (
  column("age").as[Int] and
  column("city").as[String]
)(Person)

val germanPersonReads: ColumnReads[Person] = (
  column("alter").as[Int] and
  column("stadt").as[String]
)(Person)

implicit val personReads = englishPersonReads or germanPersonReads

val englishCsv =
  """
    |age,city
    |33,London
  """.stripMargin

val germanCsv =
  """
    |alter,stadt
    |33,London
  """.stripMargin

parse[Person](englishCsv) shouldBe parse[Person](germanCsv)

SBT dependency

Package is available on Maven Central. Check the link for the latest version and add to your build.sbt:

libraryDependencies += "io.github.zamblauskas" %% "scala-csv-parser" % "<latest_version>"
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].