如何在Julia中阅读gzip压缩文件?

问题描述 投票:3回答:4

我尝试了很多库,但似乎我无法获得匹配的类型。

典型的尝试:

using SomeLib, CSV
fh = SomeLib.open("gzipped_file.gz")
CSV.read(fh) # error

例:

using CodecZlib
CSV.read(GzipDecompressorStream(open("gzipped_file.gz")))
# ERROR: MethodError: no method matching position(::TranscodingStreams.TranscodingStream{GzipDecompressor,IOStream})
julia
4个回答
5
投票

在此期间,您可以使用CSVFiles.jl:

using CSVFiles, DataFrames, FileIO

open("yourfile.csv.gz") do io
    load(Stream(format"CSV", GzipDecompressorStream(io))) |> DataFrame
end

1
投票

除了Bogumił的答案,您还可以执行以下操作:

using CSV
using GZip

df = GZip.open("some_file.csv.gz", "r") do io
    CSV.read(io)
end

0
投票

更简单:

using CSVFiles, DataFrames
df = DataFrame(load(File(format"CSV", "data.csv.gz")))

0
投票

我的新包TableReader.jl支持透明gzip,xz和zstd解压缩。因此,以下代码将按预期工作:

using TableReader

readcsv("path/to/file.csv.gz")
readcsv("path/to/file.csv.xz")
readcsv("path/to/file.csv.zst")
© www.soinside.com 2019 - 2024. All rights reserved.