in reply to Convert CSV file to TAB delimited

Something like this:

use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new( { binary => 1 } ); my $tsv = Text::CSV->new( { binary => 1, sep_char => "\t" } ); open my $infh, '<:utf8', 'input.csv' or die $!; open my $outfh, '>:utf8', 'output.tsv' or die $!; while ( my $row = $csv->getline($infh) ) { $tsv->print( $outfh, $row ); print $outfh "\n"; }

Replies are listed 'Best First'.
Re^2: Convert CSV file to TAB delimited
by Tux (Canon) on Sep 23, 2010 at 01:19 UTC

    Small nitpicks, making it even easier:

    • use the eol attribute to prevent the need for extra print calls
    • "<:encoding(utf8)" is much safer than "<:utf8", esp on input
    • use autodie to prevent all kinds of error checking (or add auto_diag => 1)
    use strict; use warnings; use autodie; use Text::CSV; my $csv = Text::CSV->new ({ binary => 1 }); my $tsv = Text::CSV->new ({ binary => 1, sep_char => "\t", eol => "\n" + }); open my $infh, "<:encoding(utf8)", "input.csv"; open my $outfh, ">:encoding(utf8)", "output.tsv"; while (my $row = $csv->getline ($infh)) { $tsv->print ($outfh, $row); }

    otherwise a great reply!


    Enjoy, Have FUN! H.Merijn