我有很多带有固定宽度字段的文本文件:
<c> <c> <c>
Dave Thomas 123 Main
Dan Anderson 456 Center
Wilma Rainbow 789 Street
Run Code Online (Sandbox Code Playgroud)
其余文件采用相似的格式,其中<c>将标记列的开头,但它们具有各种(未知)列和空间宽度.解析这些文件的最佳方法是什么?
我尝试使用Text::CSV,但由于没有分隔符,很难得到一致的结果(除非我使用模块错误):
my $csv = Text::CSV->new();
$csv->sep_char (' ');
while (<FILE>){
if ($csv->parse($_)) {
my @columns=$csv->fields();
print $columns[1] . "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
Eri*_*rom 12
正如user604939所提到的,unpack是用于固定宽度字段的工具.但是,unpack需要传递模板才能使用.由于您说您的字段可以更改宽度,因此解决方案是从文件的第一行构建此模板:
my @template = map {'A'.length} # convert each to 'A##'
<DATA> =~ /(\S+\s*)/g; # split first line into segments
$template[-1] = 'A*'; # set the last segment to be slurpy
my $template = "@template";
print "template: $template\n";
my @data;
while (<DATA>) {
push @data, [unpack $template, $_]
}
use Data::Dumper;
print Dumper \@data;
__DATA__
<c> <c> <c>
Dave Thomas 123 Main
Dan Anderson 456 Center
Wilma Rainbow 789 Street
Run Code Online (Sandbox Code Playgroud)
打印:
template: A8 A10 A*
$VAR1 = [
[
'Dave',
'Thomas',
'123 Main'
],
[
'Dan',
'Anderson',
'456 Center'
],
[
'Wilma',
'Rainbow',
'789 Street'
]
];