Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: txt to csv

by Tux (Abbot)
on Jun 24, 2013 at 06:28 UTC ( #1040375=note: print w/replies, xml ) Need Help??

in reply to txt to csv

Why only use the module for writing the CSV, and not for parsing? Parsing usually is the hard part.

If the space before the comma is to be removed on parsing, use the allow_whitespace attribute for that.

use Text::CSV; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, # Have the module complain about errors allow_whitespace => 1, # allow 1 , 2 ,3 }); my $in_file = "C:/Documents and Settings/x0199665/My Documents/tempora"; open my $in_fh, "<", $in_file or die "could not open $in_file: $!\n"; my @rows; # Use Text::CSV (or Text::CSV_XS) to parse, not split while (my $row = $csv->getline ($in_fh) { push @rows, $row; } close $in_fh or die "could not close $in_file: $!\n"; # my $rows = $csv->getline_all ($in_fh); # would do all in one go print " Please enter the name of your csv file in which you will make +modifications: "; chomp (my $out_file = <STDIN>); open my $out_fh, ">", $out_file or die "could not open $out_file: $!\n +"; $csv->eol ("\n"); $csv->print ($out_fh, $_) for @rows; close $out_fh or die "could not close $out_file: $!\n";

Enjoy, Have FUN! H.Merijn

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040375]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-06-19 14:59 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.