Daniel B
Daniel B

Reputation: 43

Perl read CSV and manipulate dates and numbers

I have run up to a problem and invested quite a few hours into understanding it...

I want to read a .csv file using perl, look for a date (and numbers), arrange the date to another format and add the numbers to an existing number.

The data in the .csv are simple fields with the ";" delimeter.

The date exists in the format 01.01.2000 in the .csv file and I would like to format ist to 2000-01-01. The amount exists in the form "-200".

Now the reading and putting it into variables works fine. However the variables dont behave like the others, I cannot properly use regex on them and if I try to add the amount to another vaiable I get the following error: Argument "\x{0}-\x{0}5\x{0},\x{0}6\x{0}6\x{0}" isn't numeric in addition (+)

Now it looks to me like there is an issue with the encoding of the data, that I did not handle correctly, but after several tries of "endode, decode, upgrade downgrade etc" I just can't get it to work. And if I save these variables to a text file and open that manually, it tells me that the file is encoded in utf8 and for read-only.

What am I missing here and what do I need to understand concerning the encoding, I read a lot about it but I still don't really get it in my case.

Edit: Sorry for my previous insufficient code. The following code should work on its own:

#!/usr/bin/perl -w
use strict;
use warnings;
use utf8;
use Text::CSV;
use Data::Dumper qw(Dumper);

my $file = "test.csv";
my $s_delimiter = ";";

open(my $fh, '<', $file) or die("ERROR open $file ($!)\n");

my $csv = Text::CSV->new({binary => 1, sep_char => $s_delimiter});

# read File
while( my $row = $csv->getline( $fh ) )
{
    my @array = @{$row};
    my $arraylength = @array;

    my $date = $array[2];
    my $amount = $array[6];

    $amount += 200;

    print "$amount\n";
    $date =~ /(\d\d).(\d\d).(\d\d\d\d)/;

    print "$date\n";
    print "$3 $2 $1\n";
}

close $fh;

Thanks for your help in advance!

Upvotes: 1

Views: 174

Answers (1)

Daniel B
Daniel B

Reputation: 43

Thanks to the comments I checked the encoding on the csv file and figured it out myself, the line

open(my $fh, '<', $file) or die("ERROR open $file ($!)\n");

had to be changed into

open(my $fh, '<:encoding(UTF-16LE)', $file) or die("ERROR open $file ($!)\n");

since the encoding is UTF-16 which I did not realise at first.

But still thank you to the comments, they were the reason I found my error :)

Upvotes: 2

Related Questions