【问题标题】:Parsing csv file and skip the first 3000 lines解析 csv 文件并跳过前 3000 行
【发布时间】:2014-07-15 00:42:20
【问题描述】:

我做了这个功能来修改我的 csv 文件:

    sub convert
{
    # open the output/input file 
my $file = $firstname."_lastname_".$age.".csv";
 $file =~ /(.+\/)(.+\.csv)/;
my $file_simple = $2;
open my $in, '<', $file or die "can not read the file: $file $!";
open my $out, '>', $outPut."_lastname.csv" or die "can not open the o file:  $!";

$_ = <$in>;

# first line
print $out "X,Y,Z,W\n";
while( <$in> )
{
    if(/(-?\d+),(-?\d+),(-?\d+),(-?\d+),(-?\d+)/)
    {
        my $tmp = ($4.$5);
        print $out $2.$sep.$3.$sep.$4.$sep.($5/10)."\n";
    }
    else
    {print $out "Error: ".$_;}
}
close $out;
}

我想跳过前 3000 行,但我不知道怎么做,这是我第一次使用 perl。

谢谢。

【问题讨论】:

  • 添加一个计数器,如果计数器
  • @Jens;我收到此错误 “strict subs”时不允许裸字“co”
  • 您在哪一行得到错误?缺少变量 co 的 my 声明。
  • @Jens;没关系,这是我的错,我不明白你的回答,谢谢 :)

标签: algorithm perl parsing csv


【解决方案1】:

由于您希望跳过前 3000 行,只需将 next ifcurrent line number variable $. 一起使用:

use strict; use warnings;

my $skip_lines = 3001;

open(my $fh, '<', 'data.dat') or die $!;
while (<$fh>) {
    next if $. < $skip_lines;
    //process the file
}
close($fh);

由于$. 检查当前行号,这个程序只是告诉 perl 从第 3001 行开始,有效地跳过了 3000 行。根据需要。

$。最后访问的文件句柄的当前行号。每个 Perl 中的文件句柄计算已读取的行数 它。 (根据 $/ 的值,Perl 关于什么构成 行可能与您的不匹配。)当从文件句柄中读取一行时(通过 readline() 或 ),或者当它调用 tell() 或 seek() 时,$. 成为该文件句柄的行计数器的别名。你可以 通过分配给 $ 来调整计数器。 ,但这实际上不会 移动搜索指针。本地化 $.不会本地化 文件句柄的行数。相反,它将本地化​​ perl 的 哪个文件句柄 $.当前别名为。美元。被重置时 文件句柄已关闭,但在重新打开打开的文件句柄时不会 没有干预 close()。有关详细信息,请参阅 I/O 运算符 佩洛普。因为 从不显式关闭,所以行号增加 跨 ARGV 文件(但请参阅 eof 中的示例)。你也可以使用 HANDLE->input_line_number(EXPR) 访问给定的行计数器 文件句柄,而不必担心你最后使用哪个句柄 访问。助记符:很多程序都使用“.”表示当前行 号码。

参考:

http://perldoc.perl.org/perlvar.html

【讨论】:

  • 谢谢,第一次使用 next if :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多