开发者

In Scala, how to read a simple CSV file having a header in its first line?

开发者 https://www.devze.com 2023-01-14 20:49 出处:网络
The task is to look for a specific field (by it\'s number in line) value by a key field value in a simple CSV file (just commas as separators, no field-enclosing quotes, never a comma inside a field),

The task is to look for a specific field (by it's number in line) value by a key field value in a simple CSV file (just commas as separators, no field-enclosing quotes, never a comma inside a field), having a header in its first line.

User uynhjl has given an example (but with a different character as a separator):


val src = Source.fromFile("/etc/passwd")
val iter = src.getLines().map(_.sp开发者_如何学Pythonlit(":"))
// print the uid for Guest
iter.find(_(0) == "Guest") foreach (a => println(a(2)))
// the rest of iter is not processed
src.close()

the question in this case is how to skip a header line from parsing?


You can just use drop:

val iter = src.getLines().drop(1).map(_.split(":"))

From the documentation:

def drop (n: Int) : Iterator[A]: Advances this iterator past the first n elements, or the length of the iterator, whichever is smaller.


Here's a CSV reader in Scala. Yikes.

Alternatively, you can look for a CSV reader in Java, and call that from Scala.

Parsing CSV files properly is not a trivial matter. Escaping quotes, for starters.


First I read the header line using take(1), and then the remaining lines are already in src iterator. This works fine for me.

val src = Source.fromFile(f).getLines

// assuming first line is a header
val headerLine = src.take(1).next

// processing remaining lines
for(l <- src) {
  // split line by comma and process them
  l.split(",").map { c => 
      // your logic here
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消