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()
}
}