2016-11-17 15 views
0

我有一些可以工作的Scala代碼,但我很難弄清楚如何以我想要的方式生成對象返回。我是Scala的新手,有很多類型,我不確定這個用例的正確答案是什麼,以及如何從輸出的數據格式化它。從CSV轉換ArrayBuffer輸出以返回Scala中最佳對象中的一行

我希望能夠調用這個函數,如getId('1.5'),並從CSV數據中獲取對象。在CSV數據的模樣:

,,ID,H2,H3,H4,H5,H6,H7,H8,H9,H10,H11,, 
0.5,0.295,17.70%,2.70%,0.00%,0.02%,6.17%,0.01%,0.00%,0.00%,32.34%,41.05%,0.00%,, 
1,0.34,18.64%,3.83%,0.00%,0.23%,7.67%,0.52%,0.00%,0.00%,30.57%,38.53%,0.00%,, 

在表

ID H2  H3 
0.5 0.295 17% 

我希望能夠用鑰匙拿到一排作爲後面的返回數據輸出如此。因此,在(使用JSON作爲一個例子),要求getId('0.5')後僞對象:

{ 
    "H2": "0.295", 
    "H3": "17%" 
} 

我希望能夠輕鬆地在其他地方使用它在我的代碼庫,所以我可以說val x = getId('1.5')。然後x.H3得到17%。它主要用於循環。這是迄今爲止我所擁有的。

def getId(id: String) = { 
    val filePath = getClass.getClassLoader.getResource("my_data.csv").getPath 

    // each row is an array of strings (the columns in the csv file) 
    val rows = ArrayBuffer[Array[String]]() 

    // (1) read the csv data 
    using(io.Source.fromFile(filePath)) { source => 
    for (line <- source.getLines) { 
     rows += line.split(",").map(_.trim) 
    } 
    } 

    // (2) print the results, here I'd actually be constructing that return object... 
    for (row <- rows) { 
    println(s"${row(0)} | ${row(2)} | ${row(3)} | ${row(4)}") 
    } 

    def using[A <: { def close(): Unit }, B](resource: A)(f: A => B): B = 
    try { 
     f(resource) 
    } finally { 
     resource.close() 
    } 
} 

回答

0

這是否符合您的需求?對你的代碼做了一些小的調整。您可以將您的數據作爲Map[String, String],並將其放入更安全的結構中,例如一個案例類。

import scala.collection.mutable 
import scala.io.Source 

object FromCSV { 
    def main(args: Array[String]) { 
    val records = readDataFromFile 
    val myRecord = records("0.5") 
    } 

    def readDataFromFile: Map[String, Record] = { 
    val filePath = getClass.getClassLoader.getResource("my_data.csv").getPath 
    val fieldSeparator = "," 
    val records = mutable.Map.empty[String, Record] 

    // each row is an array of strings (the columns in the csv file) 
    val rows = mutable.ArrayBuffer.empty[Map[String, String]] 

    // (1) read the csv data 
    using(Source.fromFile(filePath)) { source => 
     var lineNum = 0 
     var colNames = Seq.empty[String] 
     for (line <- source.getLines) { 
     lineNum += 1 
     lineNum match { 
      case 1 => colNames = line.split(fieldSeparator) 
      case _ => rows += (colNames zip line.split(fieldSeparator).map(_.trim)).toMap 
     } 
     } 
    } 

    // (2) print the results, here I'd actually be constructing that return object... 
    for (row <- rows) { 
     println(s"${row("ID")} | ${row("H2")} | ${row("H3")} | ${row("H4")}") 
     val r = Record(row("ID"), row("H2"), row("H3")) 
     records(r.id) = r 
    } 

    def using[A <: { def close(): Unit }, B](resource: A)(f: A => B): B = 
     try { 
     f(resource) 
     } finally { 
     resource.close() 
     } 
    records.toMap 
    } 
} 

case class Record(id: String, h2: String, h3: String) 
相關問題