Java 如何使用scala在读取csv文件中创建透视
我有个问题。我将创建代码scala读取csv文件,不在pivot中创建结果。有什么解决办法吗 文件csv:Java 如何使用scala在读取csv文件中创建透视,java,scala,csv,pivot,Java,Scala,Csv,Pivot,我有个问题。我将创建代码scala读取csv文件,不在pivot中创建结果。有什么解决办法吗 文件csv: Div,Person A,Excel A,Job A,Bob B,Alice B,Anna 输出: Div|Person A|Excel,Job,Bob B|Alice,Anna 我的代码: object Tes { def main(args: Array[String]): Unit = { var result : List[Person] = Nil v
Div,Person
A,Excel
A,Job
A,Bob
B,Alice
B,Anna
输出:
Div|Person
A|Excel,Job,Bob
B|Alice,Anna
我的代码:
object Tes {
def main(args: Array[String]): Unit = {
var result : List[Person] = Nil
var strings = StringBuilder.newBuilder
val bufferedSource = io.Source.fromFile("src/main/resources/config/person.csv")
for (line <- bufferedSource.getLines) {
val cols = line.split(",")
val div = cols.apply(0)
val name = cols.apply(1)
}
bufferedSource.close
}
}
如何创建类似scala的代码输出。。??谢谢。你可以这样做
val source = scala.io.Source.fromFile("src/main/resources/config/person.csv")
source.getLines()
.map(_.split(","))
.foldLeft(Map.empty[String, Seq[String]])({
case (map, Array(div, name)) =>
map + (div -> (map.get(div) match {
case None => Seq(name)
case Some(seq) => seq :+ name
}))
})
.foreach({ case (div, names) => println(div + "|" + names.mkString(",")) })
source.close()
如果你想让它更有效,你可以在折叠过程中使用可变集合 ok我将获得在模型中编写csv的完成代码。感谢@Nils 我的代码:
import java.io.{File, PrintWriter}
import scala.io.Source.fromFile
object Tes {
//Model
case class Person(div: String, person: String)
def main(args: Array[String]): Unit = {
val csv_path = "src/main/resources/config/person.csv"
val filePath = "src/main/resources/config/result-person.csv"
val process = pivot(csv_path)
println(process)
val result = saveFlattenToFile(process,filePath)
}
def pivot(path: String): List[Person] = {
//Initial
var result : List[Person] = Nil
val source = fromFile(path)
source.getLines().drop(1)
.map(_.split(","))
.foldLeft(Map.empty[String, Seq[String]])({
case (map, Array(div, name)) =>
map + (div -> (map.get(div) match {
case None => Seq(name)
case Some(seq) => seq :+ name
}))
})
.foreach({ case (div, name) =>
result = result ::: List(Person(div,name.mkString(",")))
//println(channelName + "|" + channel.mkString(","))
})
source.close()
//Output
return result
}
def saveFlattenToFile(channels: List[Person], filePath: String): Boolean = {
//Initial
var result = false
//Process
if (channels.size > 0) {
val writer = new PrintWriter(new File(filePath))
writer.write("Div|Person\n")
for (model <- channels) writer.write(model.div + "|" + model.person + "\n")
writer.close()
result = true
}
//Output
return result
}
}
这是家庭作业吗?@mpetruska没有家庭作业,但我在scala中遇到了这个问题。那么,确切的问题是什么?@mpetruska我将在代码val div=cols.apply0中按col打印,但我不能像输出那样打印。出色的工作,鲍勃。我可以通过阅读csv A、Excel A、作业输出打印列。非常感谢。。代码运行良好。。好的,我将在我的模型中实现可变集合。
import java.io.{File, PrintWriter}
import scala.io.Source.fromFile
object Tes {
//Model
case class Person(div: String, person: String)
def main(args: Array[String]): Unit = {
val csv_path = "src/main/resources/config/person.csv"
val filePath = "src/main/resources/config/result-person.csv"
val process = pivot(csv_path)
println(process)
val result = saveFlattenToFile(process,filePath)
}
def pivot(path: String): List[Person] = {
//Initial
var result : List[Person] = Nil
val source = fromFile(path)
source.getLines().drop(1)
.map(_.split(","))
.foldLeft(Map.empty[String, Seq[String]])({
case (map, Array(div, name)) =>
map + (div -> (map.get(div) match {
case None => Seq(name)
case Some(seq) => seq :+ name
}))
})
.foreach({ case (div, name) =>
result = result ::: List(Person(div,name.mkString(",")))
//println(channelName + "|" + channel.mkString(","))
})
source.close()
//Output
return result
}
def saveFlattenToFile(channels: List[Person], filePath: String): Boolean = {
//Initial
var result = false
//Process
if (channels.size > 0) {
val writer = new PrintWriter(new File(filePath))
writer.write("Div|Person\n")
for (model <- channels) writer.write(model.div + "|" + model.person + "\n")
writer.close()
result = true
}
//Output
return result
}
}