I am working on a script which takes some values from an excel and logs it to a CSV file. In between we perform some calculations on the data and then log to the CSV. I am appending a string to a value from the excel and then saving it to an array element. I am using array as to simulate an excel sheet to save execution time and finally picking values from the array and logging it to CSV.
But the problem I am facing is this values which I am getting for a column where I appended a string with a excel value is giving double quotes around. They are not visible in case of the excel but when I view that CSV in notepad++ I am getting those.
I tried looking but not getting any reason for this. Can anyone help.
Please find a code reference below:
$Array_Final[$R_FIN][11] = "Last Invoice : ".$Array_Input_DP_Plan3[$W_R][$C_LI];
my $csv = Text::CSV->new ( { binary => 1, eol => "\n" } ) # should set binary attribute.
or die "Cannot use CSV: ".Text::CSV->error_diag ();
open my $fh, ">:encoding(utf8)", $VPath."\\Temp\\".$VCsvFileName;
my @log;
for my $row(1..$#Array_Final){
#COMMENT:EMPTY THE LOG ARRAY
$#log = -1;
for my $col(1..42){
if(defined $Array_Final[$row][$col] && $Array_Final[$row][$col] ne ""){
push @log, $Array_Final[$row][$col];
}
else{
push @log, undef;
}
}
#print (@log);
$csv->print ($fh, \@log);
}
close $fh or die "new.csv: $!";
The CSV lines I am getting like this:
20161212-001,20170111,DEL,12,3,A,ABC,,,"Last Invoice : 1111",,,DP
By default, a space in a field would trigger quotation. As no rule exists this to be forced in CSV, nor any for the opposite, the default is true for safety. You can exclude the space from this trigger by setting this attribute to 0. -Text::CSV doc
Therefore setting it to 0 will fix the issue for you.
$csv->quote_space (0);
You could also specify the same while creating the object.
$csv = Text::CSV->new ({
quote_space => 0,
});