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

Hi Perl Monks, I've been knocking myself our for several hours trying to figure this one out but I can't. I'm trying to convert a csv file with several quoted items into any excel format. The quotes are killing everything that I've written. Removing the quotes then running the script gets the formatting wrong because the quoted text or numbers belong together. Here's an example of a piece of the csv file:

"Searched for: San Francisco_Microbiome" "Title","Amount","Phase","Program","Awards Year","Solicitation Year","Small Business Name","Small Business Address","Woman Owned","Principal Investigator Name","PI Phone","PI Email","Business Contact Name","BC Phone","BC Email","Abstract" "SBIR Phase I: Enhancing the skin microbiome for mosquito repellency: Next generation mosquito repellent derived from big data analysis","$225,000.00"

This is the code that I found online that I've been trying to modify with zero success:
use strict; use Spreadsheet::WriteExcel; use Text::CSV_XS; # Check for valid number of arguments if ( ( $#ARGV < 1 ) || ( $#ARGV > 2 ) ) { die("Usage: csv2xls csvfile.txt newfile.xls\n"); } # Open the Comma Separated Variable file open( CSVFILE, $ARGV[0] ) or die "$ARGV[0]: $!"; # Create a new Excel workbook my $workbook = Spreadsheet::WriteExcel->new( $ARGV[1] ); my $worksheet = $workbook->add_worksheet(); # Create a new CSV parsing object my $csv = Text::CSV_XS->new; # Row and column are zero indexed my $row = 0; while (<CSVFILE>) { if ( $csv->parse($_) ) { my @Fld = $csv->fields; my $col = 0; foreach my $token (@Fld) { $worksheet->write( $row, $col, $token ); $col++; } $row++; } else { my $err = $csv->error_input; print "Text::CSV_XS parse() failed on argument: ", $err, "\n"; } }
I thought about converting to tab delimited or pipe delimited then converting to excel but it got to be possible so I'm reaching out to the monks who are always smarter and wiser than me. Thanks!

Replies are listed 'Best First'.
Re: convert csv with quotes to xls
by Tux (Canon) on Nov 22, 2019 at 07:28 UTC

    I know you "found" the code on the internetz and did not write it yourself. Maybe it is time to find better example sites than, as this is bad code: do not mix diamond read with parse on CVS, as it will break on embedded line separators.

    Of course I have no idea what version of Text::CSV_XS you are using, but this should be simplified to something like:

    use strict; use warnings; # <-- always use strict *and* warnings use Spreadsheet::WriteExcel; use Text::CSV_XS; # Check for valid number of arguments <-- @ARGV in scalar context give +s the number of entries in the list @ARGV == 2 or die "Usage: csv2xls csvfile.txt newfile.xls\n"; # Open the Comma Separated Variable file open my $fh, "<", $ARGV[0] or die "$ARGV[0]: $!"; # <-- use lexical fi +le handles and 3-arg open # Create a new Excel workbook my $workbook = Spreadsheet::WriteExcel->new ($ARGV[1]); my $worksheet = $workbook->add_worksheet (); # Create a new CSV parsing object my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); # <--- +use diagnostics and binary # Row and column are zero indexed my $row = 0; while (my $row = $csv->getline ($fh)) { # <-- safe and fast parsing # due to auto_diag there is no reason for error-checking my $col = 0; $worksheet->write ($row, $col++, $_) for @{$r}; $row++; }

    Now, doesn't that read much easier? Also easier to extend and to maintain.

    Note that this is a basic simplified example from your code. The tools mentioned by the others also take note of dates and special formats if you want and have examples of how to deal with errors. (and csv2xlsx can merge many CSV files into a single Excel file.


    Enjoy, Have FUN! H.Merijn
Re: convert csv with quotes to xls
by marto (Cardinal) on Nov 21, 2019 at 21:26 UTC
Re: convert csv with quotes to xls
by trwww (Priest) on Nov 22, 2019 at 03:35 UTC
    As the other commenter mentioned theres utilities on the CPAN that probably do what you want, but additionally, excel can open csv files natively. Is there a particular reason you need to preprocess this file? Otherwise, just open it in excel.