gpt4 book ai didi

gzip - Perl6 : large gzipped files read line by line

转载 作者:行者123 更新时间:2023-12-04 06:14:23 26 4
gpt4 key购买 nike

我试图在 Perl6 中逐行读取 gz 文件,但是,我被阻止了:

  • How to read gz file line by line in Perl6但是,这种方法将所有内容读入:out除了非常小的文件外,使用太多的 RAM 无法使用。
  • 我不明白如何使用 Perl6 的 Compress::Zlib逐行获取所有内容,尽管我在他们的 github https://github.com/retupmoca/P6-Compress-Zlib/issues/17 上打开了一个问题
  • 我正在尝试 Perl5 的 Compress::Zlib翻译这段在 Perl5 中完美运行的代码:

  • use Compress::Zlib;
    my $file = "data.txt.gz";
    my $gz = gzopen($file, "rb") or die "Error reading $file: $gzerrno";

    while ($gz->gzreadline($_) > 0) {
    # Process the line read in $_
    }

    die "Error reading $file: $gzerrno" if $gzerrno != Z_STREAM_END ;
    $gz->gzclose() ;


    使用 Inline::Perl5在 Perl6 中:
    use Compress::Zlib:from<Perl5>;
    my $file = 'chrMT.1.vcf.gz';
    my $gz = Compress::Zlib::new(gzopen($file, 'r');
    while ($gz.gzreadline($_) > 0) {
    print $_;
    }
    $gz.gzclose();

    但我看不出如何翻译这个:(
  • 我对 Lib::Archive 示例感到困惑 https://github.com/frithnanth/perl6-Archive-Libarchive/blob/master/examples/readfile.p6我不明白如何在这里获得类似第 3 项的东西
  • 应该有
  • 之类的东西
    for $file.IO.lines(gz) -> $line {或 Perl6 中类似的东西,如果它存在,我找不到它。

    在 Perl6 中,如何在不将所有内容都读入 RAM 的情况下逐行读取大文件?

    最佳答案

    更新 现在经过测试,发现错误,现已修复。

    解决方案#2

    use Compress::Zlib;

    my $file = "data.txt.gz" ;
    my $handle = try open $file or die "Error reading $file: $!" ;
    my $zwrap = zwrap($handle, :gzip) ;

    for $zwrap.lines {
    .print
    }

    CATCH { default { die "Error reading $file: $_" } }

    $handle.close ;

    我已经用一个压缩过的小文本文件对此进行了测试。

    我对 gzip 等不太了解,但基于:
  • 认识P6;
  • 阅读 Compress::Zlib 's README 并选择 zwrap常规;
  • 查看模块的源代码,特别是 the zwrap routine 的签名our sub zwrap ($thing, :$zlib, :$deflate, :$gzip) ;
  • 反复试验,主要是猜测我需要通过:gzip副词。


  • 请评论我的代码是否适合您。我猜主要是它对于您拥有的大文件是否足够快。

    解决方案 #5 的失败尝试

    随着解决方案#2的工作,我本来希望能够只写:
    use Compress::Zlib ;
    .print for "data.txt.gz".&zwrap(:gzip).lines ;

    但这失败了:
    No such method 'eof' for invocant of type 'IO::Path'

    这大概是因为这个模块是在 IO 的重组之前编写的。类。

    这导致我到 @MattOates' IO::Handle like object with .lines ? issue .我注意到没有回应,我在 https://github.com/MattOates?tab=repositories 上没有看到相关的 repo .

    关于gzip - Perl6 : large gzipped files read line by line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54813782/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com