首页 > 其他 > 详细

2.10 csv

时间:2018-03-22 00:14:59      阅读:190      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!