I have a Map[String,Seq[Any]], and I want to serialize it to a CSV file.
example:
Map("k1"-> List(1,2,3),"k2"->List ("toto","fofo","popo"))
to
k1,k2
1,toto
2,fofo
3,popo
any suggestions ??
Something like this can be a start:
val m : Map[String, Seq[Any]] = Map("k1"-> Seq(1,2,3),"k2"->Seq("toto","fofo","popo"))
val file = new File("/path/to/output/file")
val pw = new PrintWriter(new FileWriter(file))
val header = m.keys.toList
val numLines = m(header.head).get.size
pw.println(header.mkString(","))
(0 until numLines).foreach(n => {
val line = header.map(k => m(k)(n)).mkString(",")
pw.println(line)
})
pw.close()
EDIT: I had misunderstood the question and the original answer was wrong. For another approach see LuisMiguelMejíaSuárez's link.