algorithm - 解析 csv 文件并跳过前 3000 行

标签 algorithm perl parsing csv

我做了这个函数来修改我的 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。

谢谢。

最佳答案

由于您希望跳过前 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 行。根据需要。

$. Current line number for the last filehandle accessed. Each filehandle in Perl counts the number of lines that have been read from it. (Depending on the value of $/ , Perl's idea of what constitutes a line may not match yours.) When a line is read from a filehandle (via readline() or <> ), or when tell() or seek() is called on it, $. becomes an alias to the line counter for that filehandle. You can adjust the counter by assigning to $. , but this will not actually move the seek pointer. Localizing $. will not localize the filehandle's line count. Instead, it will localize perl's notion of which filehandle $. is currently aliased to. $. is reset when the filehandle is closed, but not when an open filehandle is reopened without an intervening close(). For more details, see I/O Operators in perlop. Because <> never does an explicit close, line numbers increase across ARGV files (but see examples in eof). You can also use HANDLE->input_line_number(EXPR) to access the line counter for a given filehandle without having to worry about which handle you last accessed. Mnemonic: many programs use "." to mean the current line number.

引用:

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

关于algorithm - 解析 csv 文件并跳过前 3000 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23866771/

相关文章:

algorithm - 排序排列的最小切换

c# - 未使用排序或 Linq OrderBy 方法的无序数组中的前 K 个数字

java - 图:找到一种算法来确定矩形迷宫中从一点到另一点的最短路径?

sql - 有没有简单的方法来查询 dbix::class 结果?

c++ - 在 C++ 中解析不同类型的逗号分隔数据

c++ - 照片拼接算法。如何在给定基本图像和瓷砖列表的情况下创建马赛克照片?

perl - 如何检查Perl中是否打开了文件句柄?

perl - 是否可以使用 perl 的 vec() 函数存储 float ?

python - 使用转义的 ascii 字符串正确解析 html 页面

json - 如何从 firebase 中的键值对中获取唯一的键?