In Scala, when reading from a file, how to skip the first line?

The file is very large, so I can not save it in memory. I repeat line by line as follows

for (line <- Source.fromFile(file).getLines) { } 

How can I indicate that the first line should be skipped?

+14
scala
06 Aug 2018-11-21T00:
source share
2 answers

What about:

 for (line <- Source.fromFile(file).getLines.drop(1)) { // ... } 

drop will just push the iterator (returned by getLines ) for the specified number of elements.

+40
Aug 6 2018-11-21T00:
source share

import scala.io._

import org.json4s._

import org.json4s.jackson.JsonMethods._

import org.json4s.jackson.Serialization.write

var csvf = "/Users/keeratjohar2305/Downloads/cust.csv"

var data = Source.fromFile (csvf)

var jsonf = "/Users/keeratjohar2305/Downloads/cust.json"

val jsonWriter = new PrintWriter (new file (jsonf))

class Case Box (a: Int, b: String, c: Int, d: String, e: String, f: String, g: Int)

Box (d (0) .toInt, d (1), d (2) .toInt, d (3), d (4), d (5), d (6) .toInt)}

var data = Source.fromFile (csvf)

for (l <- data.getLines) {

if (l.split (",") (0) == "CustID") {1 == 1} else {

var data1 = csvpar (l);

variable counter = 1;

jsonWriter.write ("{\" index \ ": {\" _ id \ ": \" "+ counter +" \ "}}" + "\ n");

jsonWriter.write (write (data1) + "\ n")}

}

0
May 21 '19 at 3:22
source share



All Articles