read UTF-8 csv file with Perl

use warnings;
use strict;

my $filename = $ARGV[0] or die "Need to get CSV file on the command line\n";
my $outfile = $ARGV[1] or die "Need to get CSV file on the command line\n";

open(FH, '<:encoding(utf8)', $filename) or die $!;
open(FHOUT, '>:encoding(UTF-8)', $outfile) or die $!;

my $char;
my $read;
my $infield = 0;
my $column = 0;
my $row = 0;
my $value = "";

while($read = read FH, $char, 1){
  if ( $char eq "'" && $infield == 0 ) {
    # field begin
    $infield = 1;
    $column++;
  } elsif ( $char eq "'" && $infield == 1 ) {
    # field end
    $infield = 0;
    print "(".$value.")";
    print FHOUT $value;
    $value = "";
  } elsif ( $char eq "\n" && $infield == 0 ) {
    # end of record
    $column = 0;
    $row++;
    print FHOUT "\n";
  } elsif ( $char eq "," && $infield == 0 ) {
    # comma while not in field = separator of col
    print FHOUT "\t";
  } else {
    # its a value
    $value = $value . $char;
  }
 #   my @fields = split "," , $_;
 #  ( $fields[0] && print $fields[0] ) || print "";
}

close(FHOUT);
close(FH);
Dieser Eintrag wurde veröffentlicht in Allgemein von admin. Setze ein Lesezeichen zum Permalink.