0

We repeat the following code snippet to get the data from a csv line:

              val rowSplit = line.split(",",-1)
              rowSplit match {
                case array:Array[String] =>{
                  if (array.length > 23){
                    val (office,messageid,screenchannel,screenname) = 
(array(0),array(2),array(3), array(8))
                    ...

But it just stinks. Is there a better way to do this?

Orkun
  • 6,998
  • 8
  • 56
  • 103

2 Answers2

1

It's not easy to handle CSV file properly. Fortunately, there are some libraries out there that you can use. I used one here: http://super-csv.github.io/super-csv/index.html, which is very good.

Chong Tang
  • 2,066
  • 15
  • 12
0

First of all, note that a simple split by the separator is not a proper parsing of a general CSV file, as the values might be quoted and might contain commas.

But let's assume that in your case a comma is always a separator and the values are never quoted.

In such case you could use a regular expression to parse the CSV lines. Here a sample:

val LineRe = """([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),.*""".r
line match {
  case LineRe(office, _, messageId, screenChannel1, _, _, _, _, screenName) => ...
Gregor Raýman
  • 3,051
  • 13
  • 19