I am trying to parse the following line to an data array:
"John,Doe","123 Main St","Brown Eyes"
I wanted to have an array data
like below:
data(0) = John,Doe
data(1) = 123 Main St
data(2) = Brown Eyes
I used the following CSV parser from website:
import scala.util.parsing.combinator._
object CSV extends RegexParsers {
override protected val whiteSpace = """[ \t]""".r
def COMMA = ","
def DQUOTE = "\""
def DQUOTE2 = "\"\"" ^^ { case _ => "\"" }
def CR = "\r"
def LF = "\n"
def CRLF = "\r\n"
def TXT = "[^\",\r\n]".r
def file: Parser[List[List[String]]] = repsep(record, CRLF) <~ opt(CRLF)
def record: Parser[List[String]] = rep1sep(field, COMMA)
def field: Parser[String] = (escaped|nonescaped)
def escaped: Parser[String] = (DQUOTE~>((TXT|COMMA|CR|LF|DQUOTE2)*)<~DQUOTE) ^^ { case ls => ls.mkString("")}
def nonescaped: Parser[String] = (TXT*) ^^ { case ls => ls.mkString("") }
def parse(s: String) = parseAll(file, s) match {
case Success(res, _) => res
case _ => List[List[String]]()
}
}
Then all the spaces are trimmed. The data array actually look like:
data(0) = John,Doe
data(1) = 123MainSt
data(2) = BrownEyes
How do I avoid such unwanted "removing whitespace" for the CSV parser? Thanks!