This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Reordering CSV column in csv

2 replies
kminek
Joined: 2012-01-21,
User offline. Last seen 40 weeks 6 days ago.

Hallo all

 

I do not want to go the wrong way..

What structure is best for recording the contents of csv file and how to reorder the columns ?

Input may be  for example

col1;col3;col2; or col3;col1;col2 ( It is not known in advance)

I need to reorder the columns in the csv on col1;col2;col3

The correct order of columns can  be variable and should also be stored in any structure

Thank you very much

 

DaK

 

 

Erik Engbrecht
Joined: 2008-12-19,
User offline. Last seen 3 years 18 weeks ago.
Re: Reordering CSV column in csv
I think you want something along these lines:
scala> val firstLine = "col1;col3;col2;"firstLine: java.lang.String = col1;col3;col2;
scala> val headers = firstLine.split(";").zipWithIndex.toMapheaders: scala.collection.immutable.Map[java.lang.String,Int] = Map(col1 -> 0, col3 -> 1, col2 -> 2)
scala> val col1Idx = headers("col1")col1Idx: Int = 0
scala> val col2Idx = headers("col2")col2Idx: Int = 2
scala> val col3Idx = headers("col3")col3Idx: Int = 1
scala> val line = "c1;c3;c2"line: java.lang.String = c1;c3;c2
scala> val parts = line.split(";")parts: Array[java.lang.String] = Array(c1, c3, c2)
scala> val c1v = parts(col1Idx)c1v: java.lang.String = c1
Jesse Eichar 2
Joined: 2011-04-06,
User offline. Last seen 42 years 45 weeks ago.
Re: Reordering CSV column in csv
Well the description is a little vague.  One possible way is as follows:
  def order(col:String) = some_sorting_value     val csv = Path("pathToFile",'/')  val tmp = Path.createTempFile()  tmp.openOutput{out =>    csv.lines().foreach{line =>       line.split(",").sortBy(order).foreach {out.write(_)}    }  }  tmp.moveTo(csv, replace = true)
Where order is some sorting function to correctly order the columns.  This assumes you can order the columns based on a single column data.  It could be that you need the entire line to create sorting function.  
If you know the column mapping without need for the column or line information then you might look at Eriks method for sorting a line
It also assumes you don't need to sort the file in place.
Jesse
On Sun, Jan 22, 2012 at 6:50 PM, <daniel.kminek@centrum.cz> wrote:

Hallo all

 

I do not want to go the wrong way..

What structure is best for recording the contents of csv file and how to reorder the columns ?

Input may be  for example

col1;col3;col2; or col3;col1;col2 ( It is not known in advance)

I need to reorder the columns in the csv on col1;col2;col3

The correct order of columns can  be variable and should also be stored in any structure

Thank you very much

 

DaK

 

 


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland