Skip to content

Instantly share code, notes, and snippets.

@imanabu
Last active October 26, 2020 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imanabu/322e18507f8c5b8ec649a24280a8f8af to your computer and use it in GitHub Desktop.
Save imanabu/322e18507f8c5b8ec649a24280a8f8af to your computer and use it in GitHub Desktop.
Scala Fully Parse CSV lines from a File, supports ignoring commans in quoted strings. Can generate a JSON DTO.
package dtos
import play.api.libs.json.{Format, Json}
case class ITableRow(
cols: Seq[String]
)
object ITableRow {
implicit val jsonFormatter: Format[ITableRow] = Format(Json.reads[ITableRow], Json.writes[ITableRow])
}
// AND
package dtos
import play.api.libs.json.{Format, Json}
case class ITable(
rows: Seq[ITableRow]
)
object ITable {
implicit val jsonFormatter: Format[ITable] = Format(Json.reads[ITable], Json.writes[ITable])
}
// AND Use the following snip in your code
// -----------------------------------------------------------------------
val lines = Files.readAllLines(Paths.get(uri)).asScala
val parseCsvLine = (line: String) => {
var v = Vector.empty[String]
var inQuote = false
var s = ""
for(c:Char <- line.toCharArray) {
if (c == '"') {
inQuote = !inQuote
} else {
if (c == ',' && !inQuote) {
if (s == "") {
s = "&nbsp;"
}
v = v :+ s
s = ""
} else {
if (s.contains("________")) {
s = "________"
}
s += c
}
}
}
v = v :+ s
v
}
val rows = lines.map(line => {
val l = parseCsvLine(line)
ITableRow(l)
})
ITable(rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment