Opera and CSV is one post, that should trigger my attention :)
Nice try, but there are a few problems in this script.
- ## delimited data is not really CSV, is it?
- split works with regex's, not with strings (with " " as exception)
- There is no encoding specified
- The cols are stored multiple times
The first line of your output would be:
Id##Name##Created##Active##Mail##Icon##Id##Name##Created##Mail##Icon##
+Id##Name##Created##Mail##Icon##Id##Name##Created##Mail##Icon##Id##Nam
+e##Created##Mail##Icon##Id##Name##Created##Mail##Icon##Id##Name##Crea
+ted##Mail##Icon##Id##Name##Created##Mail##Icon##Id##Name##Created##Ma
+il##Icon##
Where my corrected (and much shorter) version will generate:
Id,Name,Created,Active,Mail,Icon
As I do not use Opera's mail, my address books are too small to see any problematic data, but what would happen is names or addresses contain diacriticals (accented letters) or the chosen delimiter character(s)? If the .adr files are stored in UTF-8, the "<" in open should be "<:encoding(utf-8)". Here's a cleaned up version that uses Text::CSV_XS for output generation.
#!/pro/bin/perl
use strict;
use warnings;
use autodie;
use Getopt::Long;
use Text::CSV_XS;
use Pod::Usage;
my $input = "";
my $output = "addresses.csv";
GetOptions (
"i|input=s" => \$input,
"o|output=s" => \$output,
);
$input or pod2usage (-message => "Missing input file name :\n", -verbo
+se => 1);
open my $in, "<", $input or die "Can not open $input ", $!, "\n";
my @chunks = split m/^#/m, do { local $/; <$in> };
close $in;
my (@table, %cols, @cols);
foreach my $chunk (@chunks) {
$chunk =~ s/CONTACT// && $chunk =~ m/\n/ or next;
my %data;
foreach my $line (split m/\n+/ => $chunk) {
$line =~ m/^\s*(.+?)\s*=\s*(.+)/ or next;
my ($col, $val) = (ucfirst lc $1, $2);
$cols{$col}++ or push @cols, $col;
$data{$col} = $val;
}
keys %data and push @table, \%data;
}
my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "\n
+" });
open my $out, ">", $output or die "Can not open $output", $!, "\n";
$csv->print ($out, \@cols);
$csv->print ($out, [ @{$_}{@cols} ]) for @table;
close $out;
__END__
=head1 NAME
operaadr2csv.pl - Converts Opera .adr files to csv file
=head1 SYNOPSIS
operaadr2csv.pl -i [inputfile] [options]
Options:
-i | input
-o | -output
=head1 DESCRIPTION
This program will convert contacts from opera browser .adr fileformat
+to
a csv list.
=head1 OPTIONS
=over 4
=item -i
Sets the name of the input file. This option is mandatory.
=item -o
Sets a name for the outputfile. The default name for the output file i
+s
addresses.csv
=back
=cut
Enjoy, Have FUN! H.Merijn
|