サーチ…


構文

  • csvReader:= csv.NewReader(r)
  • data、err:= csvReader.Read()

シンプルなCSV解析

このCSVデータを考えてみましょう:

#id,title,text
1,hello world,"This is a ""blog""."
2,second time,"My
second
entry."

このデータは、次のコードで読み取ることができます。

// r can be any io.Reader, including a file.
csvReader := csv.NewReader(r)
// Set comment character to '#'.
csvReader.Comment = '#'
for {
    post, err := csvReader.Read()
    if err != nil {
        log.Println(err)
        // Will break on EOF.
        break
    }
    fmt.Printf("post with id %s is titled %q: %q\n", post[0], post[1], post[2])
}

そして生産する:

post with id 1 is titled "hello world": "This is a \"blog\"."
post with id 2 is titled "second time": "My\nsecond\nentry."
2009/11/10 23:00:00 EOF

プレイグランド: https : //play.golang.org/p/d2E6-CGGIe



Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow