I have a list of String that I want to add a header to.
I have tried to use the code
csvLines = "latitude,longitude"
to add a header to the list, but it appends to the end of the list, not the start. I was thinking that I may have to declear the lis of string, add a heder, then map the data? Im not really sure because will not the mapping of the data replace the value of the list?
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
val input =
"[[78.836714, 16.805659], [78.85211, 16.805659], [78.862683, 16.805659], [78.869886, 16.805659], [78.869886, 16.862659], [78.864683, 16.875659], [78.859748, 16.865659], [78.857748, 16.855659], [78.857748, 16.805659], [], [78.867005, 17.000159], [78.870366, 16.977777], [78.870846, 16.937988], [78.864604, 16.905659], [78.855956, 16.933014], [78.851148, 16.985238], [78.842008, 16.990212], [78.838158, 16.945449], [78.843452, 16.895713], [], [78.869886, 17.037159], [78.869886, 17.077159], [78.869886, 17.117159], [78.869886, 17.157159], [78.869886, 17.207159], [78.869886, 17.117159], [78.836714, 17.117159], [78.85211, 17.117159], [78.862683, 17.117159], [], [78.873286, 17.267159], [78.873286, 17.256159], [78.869886, 17.256159], [78.862683, 17.256159], [78.855683, 17.256159], [78.853983, 17.248159], [78.85211, 17.256159], [78.839914, 17.256159], [78.835014, 17.256159], [78.835014, 17.267159], [], [78.836714, 17.286159], [78.85211, 17.286159], [78.862683, 17.286159], [78.869886, 17.286159], [78.869886, 17.313514], [78.861722, 17.328435], [78.85211, 17.348329], [78.837676, 17.383145], [78.85211, 17.4205], [78.860281, 17.437855], [78.869886, 17.45775], [78.869886, 17.480131], [78.860761, 17.480131], [78.851148, 17.480131], [78.837195, 17.480131], [], [78.837195, 17.527381], [78.848744, 17.552249], [78.85932, 17.57463], [78.869406, 17.599499], [78.85932, 17.619393], [78.849706, 17.636801], [78.849706, 17.554736], [78.849706, 17.636801], [78.837195, 17.659182], [], [78.836714, 17.716379], [78.845377, 17.716379], [78.853071, 17.716379], [78.861242, 17.716379], [78.871806, 17.716379], [78.862202, 17.751195], [78.853071, 17.781037], [78.843452, 17.818445], [78.837676, 17.838234], [78.845858, 17.838234], [78.855475, 17.838234], [78.862202, 17.838234], [78.870846, 17.838234], [], [78.868445, 18.027232], [78.869886, 17.955114], [78.863643, 17.902891], [78.85211, 17.897917], [78.841527, 17.930246], [78.839121, 17.979983], [78.84249, 18.027232], [78.853071, 18.027232], [78.853071, 17.977496], [], [78.869886, 18.183902], [78.869886, 18.139139], [78.869886, 18.096863], [78.863643, 18.096863], [78.854994, 18.096863], [78.854994, 18.136653], [78.854994, 18.173955], [78.854994, 18.096863], [78.845858, 18.096863], [78.838639, 18.096863], [78.838639, 18.136653], [78.838639, 18.186389], [], [78.867005, 18.402743], [78.870366, 18.380361], [78.870846, 18.340572], [78.864604, 18.308243], [78.855956, 18.335598], [78.851148, 18.387822], [78.842008, 18.382848], [78.838158, 18.338085], [78.843452, 18.288349], [], [78.838158, 18.452479], [78.84682, 18.452479], [78.854994, 18.452479], [78.863163, 18.452479], [78.870366, 18.452479], [78.863163, 18.487295], [78.856436, 18.515623], [78.84582, 18.552058], [78.838158, 18.57682], [78.84682, 18.57682], [78.855475, 18.57682], [78.863163, 18.57682], [78.870366, 18.57682], [], [78.838121, 18.646451], [78.850186, 18.646451], [78.861722, 18.646451], [78.870846, 18.646451], [], [78.870846, 18.718569], [78.861722, 18.718569], [78.854033, 18.718569], [78.846339, 18.718569], [78.838121, 18.718569], [78.838121, 18.760845], [78.838121, 18.795661], [], [78.870846, 18.847884], [78.863163, 18.847884], [78.856436, 18.847884], [78.848263, 18.847884], [78.838121, 18.847884], [78.838121, 18.880213], [78.838121, 18.922489], [], [78.870846, 19.051804], [78.870846, 19.014501], [78.870846, 18.964765], [78.865084, 18.964765], [78.854575, 18.964765], [78.854575, 19.009528], [78.854575, 19.04683], [78.854575, 18.964765], [78.848744, 18.964765], [78.838121, 18.964765], [78.838121, 19.014501], [78.838121, 19.061751], [], [78.872286, 19.186092], [78.872286, 19.243289], [78.866045, 19.273131], [78.858359, 19.245776], [78.858359, 19.213447], [78.858359, 19.186092], [78.872286, 19.186092], [78.848744, 19.186092], [78.840565, 19.186092], [78.840565, 19.230855], [78.84249, 19.270644], [78.848263, 19.293026], [78.853071, 19.270644], [78.857878, 19.245776], [], [78.838121, 19.330328], [78.851148, 19.352709], [78.861722, 19.372604], [78.874205, 19.399959], [78.861722, 19.427314], [78.851148, 19.447209], [78.851148, 19.352709], [78.851148, 19.447209], [78.838121, 19.472643], [], [78.838121, 19.51684], [78.848263, 19.51684], [78.856917, 19.51684], [78.866525, 19.51684], [78.871326, 19.51684], [78.871326, 19.580931], [78.863163, 19.598905], [78.856917, 19.574037], [78.856917, 19.551655], [78.856917, 19.51684], [78.856917, 19.574037], [78.851148, 19.591445], [78.838121, 19.6188], [], [78.838121, 19.68097], [78.855956, 19.68097], [78.864124, 19.68097], [78.873725, 19.68097], [78.864124, 19.7104], [78.855956, 19.7359], [78.838121, 19.805522], [78.857389, 19.805522], [78.866045, 19.805522], [78.874205, 19.805522], [], [78.838121, 19.955522], [78.857389, 19.955522], [78.866045, 19.955522], [78.874205, 19.955522], [], [78.874205, 20.105522], [78.866045, 20.125346], [78.857389, 20.142346], [78.838121, 20.185346], [78.857389, 20.225346], [78.866045, 20.241346], [78.874205, 20.255346], [], [78.870846, 20.365346], [78.870846, 20.328042], [78.870846, 20.278306], [78.865084, 20.278306], [78.854575, 20.278306], [78.854575, 20.323069], [78.854575, 20.360371], [78.854575, 20.278306], [78.848744, 20.278306], [78.838121, 20.278306], [78.838121, 20.328042], [78.838121, 20.375293], [], [78.838121, 20.395293], [78.848263, 20.395293], [78.856917, 20.395293], [78.866525, 20.395293], [78.871326, 20.395293], [78.871326, 20.459384], [78.863163, 20.477358], [78.856917, 20.45249], [78.856917, 20.430108], [78.856917, 20.395293], [78.856917, 20.45249], [78.851148, 20.469898], [78.838121, 20.497253], [], [78.838121, 20.527253], [78.848263, 20.527253], [78.856917, 20.527253], [78.866525, 20.527253], [78.871326, 20.527253], [78.871326, 20.588253], [78.871326, 20.600253], [78.861917, 20.640253], [78.848263, 20.640253], [78.838121, 20.600253], [78.838121, 20.527253], [], [78.870846, 20.750253], [78.870846, 20.712949], [78.870846, 20.663213], [78.865084, 20.663213], [78.854575, 20.663213], [78.854575, 20.707976], [78.854575, 20.745278], [78.854575, 20.663213], [78.848744, 20.663213], [78.838121, 20.663213], [78.838121, 20.712949], [78.838121, 20.760199], [], [78.838121, 20.810199], [78.855956, 20.810199], [78.864124, 20.810199], [78.870846, 20.810199], [78.864124, 20.839629], [78.855956, 20.865129], [78.838121, 20.909251], [78.857389, 20.909251], [78.866045, 20.909251], [78.870846, 20.909251], [], [78.873286, 20.919251], [78.873286, 20.939251], [78.869886, 20.939251], [78.862683, 20.939251], [78.855683, 20.939251], [78.853983, 20.945251], [78.85211, 20.939251], [78.839914, 20.939251], [78.835014, 20.939251], [78.835014, 20.919251]]\n"
val values = input
.trim() // remove any trailing whitespace
.drop(2) // drop the first two characters [[
.dropLast(2) // drop the last two characters ]]
.split("], [") // create a list entry for every coordinate
.filter { it.isNotBlank() } // remove blank coordinate entries, thus []
var csvLines = values.mapIndexed { index, coordinates ->
val (x, y) = coordinates.split(", ")
"$x,$y"
}
// Add header
csvLines = "latitude,longitude"
val resultFilePath: Path = Paths.get("/tmp/example.txt")
Files.write(resultFilePath, csvLines)
CodePudding user response:
val csvLines = listOf("latitude,longitude") values.mapIndexed { ...
aslo, instead of this
.trim() // remove any trailing whitespace
.drop(2) // drop the first two characters [[
.dropLast(2) // drop the last two characters ]]
you can use .trim { it in "[] " }
CodePudding user response:
You can simplify your string processing code by using a Regex
.
val csvLines = Regex("[0-9.-] ")
.findAll(input)
.chunked(2) { it[0].value "," it[1].value }
For the header, you can do:
val listWithHeader = listOf("latitude,longitude") csvLines