如何解析具有匹配大小写 class 的 csv 并将输出存储到 scala 中的 treemap[Int, List[List[InputConfig]] 对象
How to parse a csv with matching case class and store the output to treemap[Int, List[List[InputConfig]] object in scala
我正在尝试解析 csv 配置文件并将输出存储在类型为 ]> 的 treeMap 中,我在实现它时遇到错误,并且还在寻找更容易和灵活地实现我的要求的各种其他集合。
我已经为输入 csv 文件创建了一个案例 class 用于模式匹配,并且我使用配置中的一列作为每个配置的键,如果有多个配置是具有相同的密钥,则必须将配置存储为每个密钥的值列表。
case class InputConfig(A:String,
B:String,
ID:Int,
C:String,
D:String,
E:Option[String] = None);
var configTree: TreeMap[Int, List[List[InputConfig]]] = null;
def csvFileParser(fileName : String): Unit = {
for (row <- scala.io.Source.fromFile(fileName).getLines().drop(4)){
val conf = row.toString.split(",").map(_.trim)
InputConfig(conf(0),
conf(1),
conf(2).toInt,
conf(3),
conf(4),
Some(conf(5)))
configTree += (conf(2).toInt -> List[List[InputConfig]])
println(configTree)
IF the input is as given below
"1","2",1,"4","5","6"
"2","2",2,"4","5","6"
"2","2",2,"4","5","6"
"3","2",3,"4","5","6"
"4","2",4,"4","5","6"
"4","2",4,"4","5","6"
The expected output should be like this
1 -> List("1","2",3,"4","5","6")
2 -> List("2","2",3,"4","5","6"),List("2","2",2,"4","5","6")
3 -> List("2","2",3,"4","5","6")
4 -> List("2","2",3,"4","5","6"),List("4","2",4,"4","5","6")
And I should be able to access each element using the key and the column name mentioned in the case class.
And also should be able to iterate through the collections in for each key.
给你:
import scala.collection.immutable.TreeMap
object CsvParse {
def main(args: Array[String]) = {
csvFileParser(args(0))
}
var configTree: TreeMap[Int, List[InputConfig]] = TreeMap()
def csvFileParser(fileName: String): Unit = {
for (row <- scala.io.Source.fromFile(fileName).getLines()) {
val conf = row.toString.split(",").map(_.trim)
val key = conf(2).toInt
val value = InputConfig(conf(0),
conf(1),
key,
conf(3),
conf(4),
Some(conf(4)))
configTree.get(key) match {
case None => configTree += (key -> List(value))
case Some(xs) => configTree += (key -> (value :: xs))
}
}
println(configTree)
}
}
case class InputConfig(A: String,
B: String,
ID: Int,
C: String,
D: String,
E: Option[String] = None)
如果您安装了 sbt,您可以运行使用
sbt
runMain CsvParse input.dat
其中 input.dat 包含您的输入。
对于不那么脆弱的东西,您可能想要使用任何适用于 Scala 的免费 CSV 解析器。我会推荐来自 zamblauskas 的 scala-csv-parser 或来自 tototoshi 的 scala-csv。
此外,我不确定您为什么需要 conf(4) 和 Some(conf(4)) class。无论如何,您的代码都很脆弱,如果 conf(4) 不存在,将会抛出异常。在 class.
的情况下似乎没有理由包含 Some(conf(4))
我正在尝试解析 csv 配置文件并将输出存储在类型为 ]> 的 treeMap 中,我在实现它时遇到错误,并且还在寻找更容易和灵活地实现我的要求的各种其他集合。
我已经为输入 csv 文件创建了一个案例 class 用于模式匹配,并且我使用配置中的一列作为每个配置的键,如果有多个配置是具有相同的密钥,则必须将配置存储为每个密钥的值列表。
case class InputConfig(A:String,
B:String,
ID:Int,
C:String,
D:String,
E:Option[String] = None);
var configTree: TreeMap[Int, List[List[InputConfig]]] = null;
def csvFileParser(fileName : String): Unit = {
for (row <- scala.io.Source.fromFile(fileName).getLines().drop(4)){
val conf = row.toString.split(",").map(_.trim)
InputConfig(conf(0),
conf(1),
conf(2).toInt,
conf(3),
conf(4),
Some(conf(5)))
configTree += (conf(2).toInt -> List[List[InputConfig]])
println(configTree)
IF the input is as given below
"1","2",1,"4","5","6"
"2","2",2,"4","5","6"
"2","2",2,"4","5","6"
"3","2",3,"4","5","6"
"4","2",4,"4","5","6"
"4","2",4,"4","5","6"
The expected output should be like this
1 -> List("1","2",3,"4","5","6")
2 -> List("2","2",3,"4","5","6"),List("2","2",2,"4","5","6")
3 -> List("2","2",3,"4","5","6")
4 -> List("2","2",3,"4","5","6"),List("4","2",4,"4","5","6")
And I should be able to access each element using the key and the column name mentioned in the case class.
And also should be able to iterate through the collections in for each key.
给你:
import scala.collection.immutable.TreeMap
object CsvParse {
def main(args: Array[String]) = {
csvFileParser(args(0))
}
var configTree: TreeMap[Int, List[InputConfig]] = TreeMap()
def csvFileParser(fileName: String): Unit = {
for (row <- scala.io.Source.fromFile(fileName).getLines()) {
val conf = row.toString.split(",").map(_.trim)
val key = conf(2).toInt
val value = InputConfig(conf(0),
conf(1),
key,
conf(3),
conf(4),
Some(conf(4)))
configTree.get(key) match {
case None => configTree += (key -> List(value))
case Some(xs) => configTree += (key -> (value :: xs))
}
}
println(configTree)
}
}
case class InputConfig(A: String,
B: String,
ID: Int,
C: String,
D: String,
E: Option[String] = None)
如果您安装了 sbt,您可以运行使用
sbt
runMain CsvParse input.dat
其中 input.dat 包含您的输入。
对于不那么脆弱的东西,您可能想要使用任何适用于 Scala 的免费 CSV 解析器。我会推荐来自 zamblauskas 的 scala-csv-parser 或来自 tototoshi 的 scala-csv。
此外,我不确定您为什么需要 conf(4) 和 Some(conf(4)) class。无论如何,您的代码都很脆弱,如果 conf(4) 不存在,将会抛出异常。在 class.
的情况下似乎没有理由包含 Some(conf(4))