Reputation: 7248
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?
Upvotes: 0
Views: 155
Reputation: 2146
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.
Upvotes: 1
Reputation: 3081
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) => ...
Upvotes: 0