Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Scala to parse CSV data with empty columns?

Tags:

csv

scala

The raw data looks like the following:

YAPM1,20100901,23:36:01.563,Quote,,,,,,,4563,,,,,,
YAPM1,20100901,23:36:03.745,Quote,,,,,4537,,,,,,,,

The first row has extra empty columns. I parse the data as follows:

val tokens = List.fromString(line, ',')

The result:

List(YAPM1, 20100901, 23:36:01.563, Quote, 4563)
List(YAPM1, 20100901, 23:36:03.745, Quote, 4537)

At the moment there is no way of using the resulting Lists to deduce which rows had the extra columns. How do I do this?

like image 362
deltanovember Avatar asked Jul 11 '11 06:07

deltanovember


People also ask

Can you parse CSV?

Programs store CSV files as simple text characters; a comma separates each data element, such as a name, phone number or dollar amount, from its neighbors. Because of CSV's simple format, you can parse these files with practically any programming language.


1 Answers

Use string split and pass -1 as the second argument!

scala> "a,b,c,d,,,,".split(",")
res1: Array[java.lang.String] = Array(a, b, c, d)

scala> "a,b,c,d,,,,".split(",", -1)
res2: Array[java.lang.String] = Array(a, b, c, d, "", "", "", "")

FYI List fromString is deprecated in favor of string split.

like image 96
Ray Toal Avatar answered Nov 03 '22 01:11

Ray Toal