serene_monk has asked for the wisdom of the Perl Monks concerning the following question:

hello monks

I have this code for txt to csv

#!/bin/perl -w use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new(); # creates object my $in_file = 'C:\Documents and Settings\x0199665\My Documents\tempora +ry.tl'; # input file open my $in_fh, '<', $in_file or die "could not open $in_file: $!\n"; + # assigns the file handler to input file as in_fh my @rows = (); + # keeps the count of rows in txt input file while( my $line = <$in_fh> ){ + # copies the line from our input chomp $line; push @rows, [ split m{ ,}msx, $line ]; + # splits the contents of $line seperated by comma and pushes it i +n array } close $in_fh or die "could not close $in_file: $!\n"; $csv->eol( "\n" ); + # after each line it gives a new line command print " Please enter the name of your csv file in which you will make +modifications "; print "\n"; my $out_file = <>; chomp $out_file; open my $out_fh ,">", $out_file or die "could not open $out_file: $!\n +"; for my $row ( @rows ){ $csv->print( $out_fh, $row ) or die "could not print to $out_file: $ +!\n"; # prints out the csv format } close $out_fh or die "could not close $out_file: $!\n"; my $rm_file = "temporary.tl"; unlink($rm_file) ; # this will remove t +he temporary file exit;

but this code puts " " in the csv . I don't know why , so how to remove them

Replies are listed 'Best First'.
Re: txt to csv
by Tux (Canon) on Jun 24, 2013 at 06:28 UTC

    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 +ry.tl"; 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
Re: txt to csv
by davido (Cardinal) on Jun 24, 2013 at 05:16 UTC

    What does your input data look like? Are there space characters embedded in the fields? Null fields? Fields with characters >= 0x7f?

    See the "quote_space", "quote_null" and "quote_binary" attributes that can be passed along to the constructor, documented in Text::CSV's POD.


    Dave

      Hey thank you for helping out

      yes my file has spaces and special characters in it , if you could please point out the modification in code I will be grateful

        Which part are you having trouble with: Adding parameters to the constructor call, or understanding the portion of the POD that explains what parameters to add? Who wrote the code you're currently using?

        The line you need to modify is line 7. You'll be adding parameters to your call to the constructor (the new() method).


        Dave

Re: txt to csv
by kcott (Archbishop) on Jun 24, 2013 at 05:44 UTC

    G'day serene_monk,

    Good to see you've made progress since: CSV SPLIT.

    In the documentation for Text::CSV, take a look at the attributes available for the constructor (i.e. new (\%attr)) in the FUNCTIONS section.

    Without seeing your data (except for " " in isolation), I'd guess quote_char would be the one that directly relates to this:

    "The char used for quoting fields containing blanks, by default the double quote character ("). A value of undef suppresses quote chars. ..."

    There are quite a few other attributes relating to quoting, whitespace in fields, etc., so have a read through them all. The defaults you'll get from using no attributes (i.e. my $csv = Text::CSV->new();) are shown near the end of that section.

    -- Ken