英文:
How do I read a gzipped CSV file?
问题
我正在面临阅读存档的CSV文件时遇到一些问题。
所以我想使用csv
包和gzip
包来解决这个问题,但我不知道如何将它们结合起来使用。
gzip.Reader(p []bytes)
和csv.Reader()
具有不同的签名。
这是我的读取函数:
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))
}
}
}
你有什么建议吗?
英文:
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 ?
答案1
得分: 31
只需打开文件进行读取,然后使用该文件句柄进行gzip压缩,再将该文件句柄用于csv读取器:
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)
}
}
这是我的(未压缩的)data.csv
文件:
"foo","bar","baz"
程序的输出结果是:
foo
bar
baz
如预期所示。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论