excel - 如何使用 Power Query 解压缩 GZ 文件?

标签 excel gzip powerquery data-analysis m

对于 zip 文件,我发现了这个 code还有这个tutorial解释了如何使用该代码并且它运行良好。

问题是我有 gz 文件,而不是 zip 文件。 gz 文件包含 csv

所以我尝试更新一些值,例如使用 Compression.GZip而不是 Compression.Deflate 但这没有帮助。我仍然得到一张空 table 。

(GZFile) =>
let
    Header = BinaryFormat.Record([
        MiscHeader = BinaryFormat.Binary(14),
        BinarySize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
        FileSize   = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
        FileNameLen= BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian),
        ExtrasLen  = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian)    
    ]),
 
    HeaderChoice = BinaryFormat.Choice(
        BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
        each if _ <> 67324752             // not the IsValid number? then return a dummy formatter
            then BinaryFormat.Record([IsValid = false, Filename=null, Content=null])
            else BinaryFormat.Choice(
                    BinaryFormat.Binary(26),      // Header payload - 14+4+4+2+2
                    each BinaryFormat.Record([
                        IsValid  = true,
                        Filename = BinaryFormat.Text(Header(_)[FileNameLen]),
                        Extras   = BinaryFormat.Text(Header(_)[ExtrasLen]),
                        Content  = BinaryFormat.Transform(
                            BinaryFormat.Binary(Header(_)[BinarySize]),
                            // (x) => try Binary.Buffer(Binary.Decompress(x, Compression.Deflate)) otherwise null
                            (x) => try Binary.Buffer(Binary.Decompress(x, Compression.GZip)) otherwise null
                        )
                        ]),
                        type binary                   // enable streaming
                )
    ),
 
    ZipFormat = BinaryFormat.List(HeaderChoice, each _[IsValid] = true),
 
    Entries = List.Transform(
        List.RemoveLastN( ZipFormat(GZFile), 1),
        (e) => [FileName = e[Filename], Content = e[Content] ]
    )
in
    Table.FromRecords(Entries)

你们能告诉我如何修改代码吗?

最佳答案

您可以按如下方式读取 GZIP 中的 CSV:

let
    Source = Binary.Decompress(File.Contents("C:\Users\Dav\Downloads\test.gz"), Compression.GZip),
    #"Imported CSV" = Csv.Document(Source,[Delimiter=",", Encoding=1252])

in
    #"Imported CSV"

您还可以阅读 Chris Webb 的 blog 的更多信息。 .

关于excel - 如何使用 Power Query 解压缩 GZ 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77785844/

相关文章:

excel - 子函数内的用户表单

vba - 检查工作表中是否有公式

python - 如何使用子进程在python中解压gz文件

java - 在 tomcat servlet 上使用服务器发送事件进行 gzip/deflate 压缩

excel - Power Query List.RemoveNulls 列集合

excel - 不同工作表上具有范围标准的 COUNTIFS

excel - 如何使用Excel的 "=SUMPRODUCT(--ISNUMBER(SEARCH..."函数排除 'complicated compounded'结果?

linux - gzip 在脚本中已经有 gz 后缀不变

excel - 文本/字符串上的 DAX LOOKUPVALUE

excel - 用户定义的连接