我做了这个函数来修改我的 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 if
与 current 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.
引用:
关于algorithm - 解析 csv 文件并跳过前 3000 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23866771/