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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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) => ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.