Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I read a gzipped CSV file?

Tags:

csv

gzip

go

I'm facing some issues in reading archived CSV files.

So I want to use the csv package and the gzip package for this, but I don't know how to combine them.

The gzip.Reader(p []bytes) and the csv.Reader() have different signatures.

This is my reader function:

func reader(filename string, c chan string) {
    fi, err := os.Open(filename)
    var r *bufio.Reader
    if err != nil {
        fmt.Println("%q",err)
        os.Exit(1)
    }

    fz, err := g.NewReader(fi)

    if err != nil {
        r = bufio.NewReader(fi)
    }else {
        r = bufio.NewReader(fz)
    }


    for {
        line, err := r.ReadString('\n')
        if err != nil {
            fmt.Println("done reading file")
            c <- "done"
            break
        }else{
            c <- fmt.Sprintf("%q",strings.Fields(line))
        }
    }
} 

Do you have any suggestions ?

like image 688
dan89 Avatar asked Jul 10 '14 09:07

dan89


1 Answers

Just Open the file for reading, then use that file handle with gzip and then use that file handle for the csv reader:

package main

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

func main() {
    f, err := os.Open("data.csv.gz")
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()
    gr, err := gzip.NewReader(f)
    if err != nil {
        log.Fatal(err)
    }
    defer gr.Close()

    cr := csv.NewReader(gr)
    rec, err := cr.Read()
    if err != nil {
        log.Fatal(err)
    }
    for _, v := range rec {
        fmt.Println(v)
    }
}

and this is my (uncompressed) data.csv:

"foo","bar","baz"

The output of my program is:

foo
bar
baz

as expected.

like image 93
topskip Avatar answered Oct 10 '22 11:10

topskip