首页 > 解决方案 > Scala how to format scrambled data in proper order

问题描述

I have my data in text file which looks as follows

a,b,c,"d
ee"
1,2,3,"fo
ur"
p,o,t,"lu
ck"
o,n,e,"m
o
re"

I want to clean my data in such a way that my final output should be as follows:

a,b,c,"dee"
1,2,3,"four"
p,o,t,"luck"
o,n,e,"more"

This what I tried but I cant get what I was expecting:

val clean = Source.fromFile("my/path/csv/file.csv")
  .getLines
  .drop(1)
  .mkString
  .split("\"")
  .array

Can someone help me how to do this?

标签: scala

解决方案


If your file is not too big:

Source.fromFile("my/path/csv/file.csv")
  .mkString                               // Iterator[String] to String
  .init                                   // Remove the last " as we're gooing to split on \"\n and the last one won't be removed
  .split("\"\n")                          // "a,b,c,\"d\nee\"\n1,2,3,\"fo becomes Array("a,b,c,\"d\nee", "1,2,3,\"fo")
  .map(_.replace("\n", "") + "\"")        // and we remove those wrongly placed \n

推荐阅读