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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With