Thread CSV umwandeln (38 answers)
Opened by lukastonner at 2010-06-18 16:31

pq
 2010-06-18 18:49
#138476 #138476
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
also:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ftp->get($filename, $local_file);
open my $fh, '<', $local_file or die "Could not open $local_file: $!";
open my $ofh, ">", $local_file_converted or die "Could not open $local_file_converted: $!";

# wenn Text::CSV
use Text::CSV;
my $csv = Text::CSV->new();
my $ocsv = Text::CSV->new({ sep_char => ';' });
# ---

while (my $line = <$fh>) {

# wenn Text::CSV

    my $status  = $csv->parse($line); # ggfs. fehler abfangen
    my @columns = $csv->fields();

    $status = $ocsv->combine(@columns); # ggfs. fehler abfangen
    $line   = $ocsv->string();

# wenn manuell

    $line =~ tr/,/;/;

# ---

    print $ofh $line;
}
close $fh;
close $ofh;


(ungetestet)

edit: semikolon statt punkt
Last edited: 2010-06-18 18:59:47 +0200 (CEST)
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread CSV umwandeln