1. 程式人生 > >2.10 csv

2.10 csv

gpo maria moni rec print ani fun panic bre

data.csv

"Name","Surname","Age"
# this is comment in data
"John","Mnemonic",20
Maria,Tone,21
package main

import (
    "encoding/csv"
    "fmt"
    "os"
)

func main() {

    file, err := os.Open("data.csv")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    reader := csv.NewReader(file)
    reader.FieldsPerRecord = -1
reader.Comment = ‘#‘ for { record, e := reader.Read() if e != nil { fmt.Println(e) break } fmt.Println(record) } } /* [Name Surname Age] [John Mnemonic 20] [Maria Tone 21] EOF */

un_comment.csv

Name;Surname;Age
"John";Mnemonic;20
"Maria";Tone;21
package main

import (
    "encoding/csv"
    "fmt"
    "os"
)

func main() {

    file, err := os.Open("data_uncommon.csv")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    reader := csv.NewReader(file)
    reader.Comma = ‘;‘

    for {
        record, e := reader.Read()
        if
e != nil { fmt.Println(e) break } fmt.Println(record) } } /* [Name Surname Age] [John Mnemonic 20] [Maria Tone 21] EOF */

2.10 csv