Scala 将CSV文件读取到OrderedMap

Scala 将CSV文件读取到OrderedMap,scala,Scala,我正在读取CSV文件,并将数据添加到Scala中的Map val br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName)), "UTF-8")) val inputFormat = CSVFormat.newFormat(delimiter.charAt(0)).withHeader().withQuote('"')

我正在读取
CSV
文件,并将数据添加到Scala中的
Map

 val br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName)), "UTF-8"))
 val inputFormat = CSVFormat.newFormat(delimiter.charAt(0)).withHeader().withQuote('"')                                
 import scala.collection.JavaConverters._
 import org.apache.commons.csv.{CSVFormat, CSVParser}

  val csvRecords = new CSVParser(br, inputFormat).getRecords.asScala
  val buffer = for (csvRecord <- csvRecords; if csvRecords != null && csvRecords.nonEmpty)
    yield csvRecord.toMap.asScala                              
    buffer.toList                                                  
val br=new BufferedReader(新的InputStreamReader(新文件InputStream(新文件名)),“UTF-8”))
val inputFormat=CSVFormat.newFormat(delimiter.charAt(0)).withHeader().withQuote(“”)
导入scala.collection.JavaConverters_
导入org.apache.commons.csv.{CSVFormat,CSVParser}
val csvRecords=新的CSVParser(br,inputFormat).getRecords.asScala

val buffer=for(csvRecord如果我正确理解您的问题,这里有一种方法可以创建
LinkedHashMap
s列表,并按顺序排列元素:

 val br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName)), "UTF-8"))
 val inputFormat = CSVFormat.newFormat(delimiter.charAt(0)).withHeader().withQuote('"')                                
 import scala.collection.JavaConverters._
 import org.apache.commons.csv.{CSVFormat, CSVParser}

  val csvRecords = new CSVParser(br, inputFormat).getRecords.asScala
  val buffer = for (csvRecord <- csvRecords; if csvRecords != null && csvRecords.nonEmpty)
    yield csvRecord.toMap.asScala                              
    buffer.toList                                                  
// Assuming your CSV File has the following content:
fname,lname,grade
John,Doe,A
Ann,Cole,B
David,Jones,C
Mike,Duke,D
Jenn,Rivers,E

import collection.mutable.LinkedHashMap

// Get indexed header from CSV
val indexedHeader = io.Source.fromFile("/path/to/csvfile").
  getLines.take(1).next.
  split(",").
  zipWithIndex

indexedHeader: Array[(String, Int)] = Array((fname,0), (lname,1), (grade,2))

// Aggregate LinkedHashMap using foldLeft
val ListOfLHM = for ( csvRecord <- csvRecords ) yield    
  indexedHeader.foldLeft(LinkedHashMap[String, String]())(
    (acc, x) => acc += (x._1 -> csvRecord.get(x._2))
  )

ListOfLHM: scala.collection.mutable.Buffer[scala.collection.mutable.LinkedHashMap[String,String]] = ArrayBuffer(
  Map(fname -> John, lname -> Doe, grade -> A),
  Map(fname -> Ann, lname -> Cole, grade -> B),
  Map(fname -> David, lname -> Jones, grade -> C),
  Map(fname -> Mike, lname -> Duke, grade -> D),
  Map(fname -> Jenn, lname -> Rivers, grade -> E)
)
//假设您的CSV文件包含以下内容:
fname,lname,grade
约翰,多伊,一个
安,科尔,B
大卫,琼斯,C
迈克,杜克,D
詹,里弗斯,东
导入collection.mutable.LinkedHashMap
//从CSV获取索引头
val indexedHeader=io.Source.fromFile(“/path/to/csvfile”)。
拿(1)个,下一个。
拆分(“,”)。
zipWithIndex
indexedHeader:Array[(String,Int)]=数组((fname,0),(lname,1),(grade,2))
//使用foldLeft聚合LinkedHashMap
val ListOfLHM=for(csvRecord acc+=(x.\U 1->csvRecord.get(x.\U 2))
)
ListOfLHM:scala.collection.mutable.Buffer[scala.collection.mutable.LinkedHashMap[String,String]=ArrayBuffer(
地图(fname->John,lname->Doe,grade->A),
地图(fname->Ann,lname->Cole,grade->B),
地图(fname->David,lname->Jones,grade->C),
地图(fname->Mike,lname->Duke,grade->D),
地图(fname->Jenn,lname->Rivers,grade->E)
)

使用样本数据和预期输出更新问题请编辑样本输出的问题。我希望它会很清楚。您是否只使用fname、lname输入文件contalns数据?否。它有100列。为了简化,我只提到了2列。