Comment detail

RFC 4180対応版 CSVレコードの分解 (Nested Flatten)
寝る前にもいっちょ。今度は自分でパース。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import scala.collection.mutable.ListBuffer
def parseCSV(s:String):Array[Array[String]] = {
  def split(s:String, c:String) = {
    val buf = new ListBuffer[String]
    val result = new ListBuffer[String]
    s.split(c).foreach(p => p.filter(_ =='"').length%2 match{
      case 0 if  buf.isEmpty  => result += p
      case 0 if !buf.isEmpty  => buf    += p
      case 1 if  buf.isEmpty  => buf    += p
      case 1 if !buf.isEmpty  =>
        buf += p
        result += buf.mkString(c)
        buf.clear
    })
    result
  }
  split(s,"\n").map(line => {
    split(line, ",").map(col => {
      col.replaceAll("\"\"", "\"").replaceAll("^(\")", "")
         .replaceAll("(\")$", "")
    }).toArray
  }).toArray
}

val data = """"aaa","b
bb","ccc",zzz,"y""Y""y",xxx"""

parseCSV(data).foreach(line => {
  (1 to line.length).foreach(i => {
    println(i + " => " + line(i-1))
  })
})

Index

Feed

Other

Link

Pathtraq

loading...