Saturday 15 September 2012

How can I read a CSV file and put its content in a Map in Scala? -


I have a CSV file, in which the data matrix is. The first column of this matrix has a label and the value in the other column Are related to the label (i.e. at the first column). Now I want to read this CSV file and insert the data into Scala in the map [string, array]. The key label of the map (it should be in the first column) and the map values ​​should be of other values ​​(in the rest of these columns). To read the CSV file I use Opansisvi

  Val ISR: InputStreamReader = new InputStreamReader (getClass.getResourceAsStream ( "test.csv")) `val data: IndexedSeq [array [string ]] = New CSVReader (ISR) .readAll.asScala .toIndexedSeq`   

Now I have a data in IndexedSeq [Array [String]] . Can I use this functional method or should I choose better because it can be complicated to read all the data at once? Okay, now I have to make a map from this indexed SEC. For this, I map indexed SEC to an indexed SEK of Tupel [string, array [string]] to separate the label value from the rest of the values ​​and then make this map.

  val Results: Map [string, arrays (string) = data. Filter (E => EISAT) .map (E = > (E. Head, et al.). / Code>  

This works for small instances, but when I use it to read the contents of my CSV file it throws a java.lang. Rantime exception I also tried to create a map with a group or tried to make several maps (one for each line) and later tried to reduce them on a larger map, but without success I also read one more post on Stakeoverflow and someone assumed that there's the complexity of O (NA²) in the toMap. I got it at the end of my StackTrace (the entire stacktrace is quite long).

  formula exception "main" sun.reflect.NativeMethodAccessorImpl.invoke0 java.lang.reflect.InvocationTargetException (Native java.lang.reflect.Method.invoke (on Method.java:601 Sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)) on Org.jetbrains.plugins.scala.testingSupport.specs2.JavaSpecs2Runner.runSingleTest () Sun.reflect on org.jetbrains.plugins.scala.testingSupport.specs2.JavaSpecs2Runner.main (JavaSpecs2Runner.java:76) at JavaSpecs2Runner.java:130) NativeMethodAccessorImpl.invoke0 sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:57) (Native Method On sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke) (Method . Java: 601) due to com.intellij.rt.execution.application.AppMain.main (AppMa in.java:120): java.lang.RuntimeException: org.specs2.specification.SpecificationStructure $ 27): scala. on sys.package $ .error (package.scala com.test.MyClassSpec: can not create specification .createSpecification (BaseSpecification.scala: 96) on org.specs2.runner.ClassRunner.createSpecification (ClassRunner.scala: 64) org. on specs2.runner.ClassRunner.start (ClassRunner.scala: 35) on org.specs2.runner. ClassRunner.main (ClassRunner.scala: 28) at org.specs2.runner.NotifierRunner.main (Notifi Runner Scala: 24) ... 11 additional clearance code ending process 1   with 

Does anyone know a way to make a map of the data in a CSV file?

This has worked for me:

  Import Slaela. IO .source source.frame ("some_for_big_file"). Meets. Map (_ divided (";"). Calculation (_ = & gt; true)   

partition breaks each line of CSV file in a simple record. The count is only to check if the file is actually read.

So now we can read it in a real CSV file (though I only make it a little File is tested):

  scala> val content = source.fromFile ("test.csv"). GetLines.map (_. Partition (";") content: iterator [ Array [java.lang.String]] = Non-empty eater scale & gt; Val header = content.next header: array [java.lang.String] = array (element, duration) scale & gt; Content.map (header.zip (_) toMap) res40: iterator [Scala.collection.immutable.Map [java.lang.String, java.lang.String]] = non blank etter   

works quite well with simple csv files If you have more complex people (for example, there are entries on multiple lines), then you may have to use a more complicated CSV parser (for example, but usually F. PR also will iterator of some sort and you can use the same map (... zip ...) function.

No comments:

Post a Comment