douquanqiao6788 2019-07-01 08:14 采纳率: 100%
浏览 7203
已采纳

读取CSV文件时如何跳过第一行?

我有一个稍复杂的csv文件,我需要跳过第一行来阅读它。 我正在使用的Python / Pandas却轻松做到这一点:
df = pd.read_csv(filename, skiprows=1)

但我不知道如何在Go中进行操作。

package main

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

type mwericsson struct {
    id     string
    name   string
    region string
}

func main() {

    rows := readSample()

    fmt.Println(rows)
    //appendSum(rows)
    //writeChanges(rows)
}

func readSample() [][]string {
    f, err := os.Open("D:/in/20190629/PM_IG30014_15_201906290015_01.csv")
    if err != nil {
        log.Fatal(err)
    }
    rows, err := csv.NewReader(f).ReadAll()
    f.Close()
    if err != nil {
        log.Fatal(err)
    }
    return rows
}

Error:

2019/07/01 12:38:40 record on line 2: wrong number of fields

PM_IG30014_15_201906290015_01.csv:

PTN Ethernet-Port RMON Performance,PORT_BW_UTILIZATION,2019-06-29 20:00:00,33366     
DeviceID,DeviceName,ResourceName,CollectionTime,GranularityPeriod,PORT_RX_BW_UTILIZATION,PORT_TX_BW_UTILIZATION,RXGOODFULLFRAMESPEED,TXGOODFULLFRAMESPEED,PORT_RX_BW_UTILIZATION_MAX,PORT_TX_BW_UTILIZATION_MAX
3174659,H1095,H1095-11-ISM6-1(to ZJBSC-V1),2019-06-29 20:00:00,15,22.08,4.59,,,30.13,6.98
3174659,H1095,H1095-14-ISM6-1(to T6147-V),2019-06-29 20:00:00,15,2.11,10.92,,,4.43,22.45
  • 写回答

2条回答 默认 最新

  • duanhe6718 2019-07-01 12:05
    关注

    skip the first row when reading a csv file


    For example,

    package main
    
    import (
        "bufio"
        "encoding/csv"
        "fmt"
        "io"
        "os"
    )
    
    func readSample(rs io.ReadSeeker) ([][]string, error) {
        // Skip first row (line)
        row1, err := bufio.NewReader(rs).ReadSlice('
    ')
        if err != nil {
            return nil, err
        }
        _, err = rs.Seek(int64(len(row1)), io.SeekStart)
        if err != nil {
            return nil, err
        }
    
        // Read remaining rows
        r := csv.NewReader(rs)
        rows, err := r.ReadAll()
        if err != nil {
            return nil, err
        }
        return rows, nil
    }
    
    func main() {
        f, err := os.Open("sample.csv")
        if err != nil {
            panic(err)
        }
        defer f.Close()
        rows, err := readSample(f)
        if err != nil {
            panic(err)
        }
        fmt.Println(rows)
    }
    

    Output:

    $ cat sample.csv
    one,two,three,four
    1,2,3
    4,5,6
    $ go run sample.go
    [[1 2 3] [4 5 6]]
    $ 
    
    $ cat sample.csv
    PTN Ethernet-Port RMON Performance,PORT_BW_UTILIZATION,2019-06-29 20:00:00,33366     
    DeviceID,DeviceName,ResourceName,CollectionTime,GranularityPeriod,PORT_RX_BW_UTILIZATION,PORT_TX_BW_UTILIZATION,RXGOODFULLFRAMESPEED,TXGOODFULLFRAMESPEED,PORT_RX_BW_UTILIZATION_MAX,PORT_TX_BW_UTILIZATION_MAX
    3174659,H1095,H1095-11-ISM6-1(to ZJBSC-V1),2019-06-29 20:00:00,15,22.08,4.59,,,30.13,6.98
    3174659,H1095,H1095-14-ISM6-1(to T6147-V),2019-06-29 20:00:00,15,2.11,10.92,,,4.43,22.45
    $ go run sample.go
    [[DeviceID DeviceName ResourceName CollectionTime GranularityPeriod PORT_RX_BW_UTILIZATION PORT_TX_BW_UTILIZATION RXGOODFULLFRAMESPEED TXGOODFULLFRAMESPEED PORT_RX_BW_UTILIZATION_MAX PORT_TX_BW_UTILIZATION_MAX] [3174659 H1095 H1095-11-ISM6-1(to ZJBSC-V1) 2019-06-29 20:00:00 15 22.08 4.59   30.13 6.98] [3174659 H1095 H1095-14-ISM6-1(to T6147-V) 2019-06-29 20:00:00 15 2.11 10.92   4.43 22.45]]
    $
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 labelme打不开怎么办
  • ¥35 按照图片上的两个任务要求,用keil5写出运行代码,并在proteus上仿真成功,🙏
  • ¥15 免费的电脑视频剪辑类软件如何盈利
  • ¥30 MPI读入tif文件并将文件路径分配给各进程时遇到问题
  • ¥15 pycharm中导入模块出错
  • ¥20 Ros2 moveit2 Windows环境配置,有偿,价格可商议。
  • ¥15 有关“完美的代价”问题的代码漏洞
  • ¥15 请帮我看一下这个简易化学配平器的逻辑有什么问题吗?
  • ¥15 暴力法无法解出,可能要使用dp和数学知识
  • ¥15 wpf通过绑定控件自身的值,来实现背景颜色的切换