Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
Don't ask to ask, just ask
 
PerlMonks  

Converting a text file to a csv file

by $new_guy (Acolyte)
on Sep 28, 2010 at 07:38 UTC ( #862364=perlquestion: print w/ replies, xml ) Need Help??
$new_guy has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I would like to convert a text file to .csv file. My code is:
#!/usr/bin/perl use strict; use warnings; use Text::CSV; my $file = 're-organized.txt'; my $csv = Text::CSV->new(); #now make a file for the ouput my $outputfile5 = "re-organized.csv"; if (! open(POS, ">>$outputfile5") ) { print "Cannot open file \"$outputfile5\" to write to!!\n\n +"; exit; } open (CSV, "<", $file) or die $!; while (<CSV>) { if ($csv->parse($_)) { my @columns = $csv->fields(); print POS "@columns\n"; } else { my $err = $csv->error_input; print "Failed to parse line: $err"; } } close CSV;
The file I am trying to convert is:
0 z z z z z z z z + z z z z z z z z z z z z z z z z + z - z z z z z z z z z z z z z z + z z z z z z z z z z z z z z z z z z + z z z z z z z z z z z z z z z z z z +z z z z z z z z z z z z z z z z z z z + z 0 z z z z z z - z + - z z z z z z - z z z z z + - z z - - z - z z z z + - z z z z z z z z - z z z + - z z z - - z - z z z z + z z z z z z z z z z z z z z z z z + - z z z z z - z z - z z z z + z z z z z z z z

I encounter a problem! The script does not separate the elements/values in each row into separate columns in the .csv spreadsheet. How can I resolve this so that I have each value in the rows in a separate column in the generted .csv file?

thanks

Comment on Converting a text file to a csv file
Select or Download Code
Re: Converting a text file to a csv file
by suhailck (Friar) on Sep 28, 2010 at 07:56 UTC
    I think you need to split each line into an array and pass the array reference to print into the out file.
    my $csv = Text::CSV->new ({ binary => 1, eol => "\n" }); open(my $out,">>",$outfile); open (CSV, "<", $file) or die $!; while(<CSV>) { my @array=split /\s+/; $csv->print($out,\@array); }
      Hi Suhailck,

      It works perfectly! Thanks!!

Re: Converting a text file to a csv file
by bart (Canon) on Sep 28, 2010 at 08:43 UTC
    It's not clear to me how you want to process the source file. Do you want one character per cell? I guess you do.
    open TXT, "<", $file) or die "Cannot open $file: $!"; while(<TXT>) { chomp; my @array = split ''; # 1 char per array item s/\s// foreach @array; # replace space with empty string ... }
    The "..." must be replaced with whatever you have to do to output the row to the CSV file, for example from the code supplied by suhailck in Re: Converting a text file to a csv file.
      Hi Bart,

      I doesn't seem to work for me! It still gives the same output!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://862364]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-04-18 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (472 votes), past polls