码迷,mamicode.com
首页 > 其他好文 > 详细

2.10 csv

时间:2018-03-22 00:28:42      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:for   dsp   imp   logs   gpo   eof   package   pre   coding   

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

标签:for   dsp   imp   logs   gpo   eof   package   pre   coding   

原文地址:https://www.cnblogs.com/zrdpy/p/8620699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!