Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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]
[ambrus]: My boss wants to generate output data that is impossible to compute from the inputs we have. And I'm trying to convince him that he shouldn't do that.
[Corion]: ambrus: He isn't familiar with GIGO (or nIGO) yet?
[Corion]: Also, is it impossible in the general case, but doable in your specific case, maybe? I find that working through a counterexample usually makes people see the light

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2017-12-12 13:01 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (332 votes). Check out past polls.