- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Reordering CSV column in csv
Sun, 2012-01-22, 18:50
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
Sun, 2012-01-22, 20:31
#2
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:
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
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