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

How to write empty double quoted string "" in SCV using Text::CSV? Resuils of "" or "\"\"" are same """"""

Replies are listed 'Best First'.
Re: Double quotes in CSV using Text::CSV
by Corion (Patriarch) on May 19, 2015 at 07:31 UTC

    You don't show any relevant code, so I have to guess as to what you tried and what your expected output is.

    My guess is that you want as output ,"", while Text::CSV in its standard configuration outputs ,, for an empty column. The documentation shows how to make Text::CSV always use quotes when creating a CSV line:

    #!perl -w use strict; use Text::CSV; my $csv= Text::CSV->new({ binary => 1, eol => $/ }); my $csv_quoted= Text::CSV->new({ binary => 1, eol => $/, always_quote +=> 1 }); my @values= ('A string', 'A string with "double quotes"', '""', 'Now a +n empty string', '', '<< here'); $csv->print( \*STDOUT, \@values ); $csv_quoted->print( \*STDOUT, \@values ); __END__ "A string","A string with ""double quotes""","""""","Now an empty stri +ng",,"<< here" "A string","A string with ""double quotes""","""""","Now an empty stri +ng","","<< here"
Re: Double quotes in CSV using Text::CSV
by Anonymous Monk on May 19, 2015 at 07:32 UTC

    Are you perhaps looking for something like this?

    use Text::CSV; my $csv = Text::CSV->new({binary=>1,always_quote=>1,eol=>$/}); $csv->print(\*STDOUT, ["","",""]); __END__ "","",""
      it's works, but always_quote=>1 it is no good. necessary for me:
      $csv->print(\*STDOUT, ["1","2",""]); __END__ 1,2,""

        I don't think this is possible with Text::CSV or Text::CSV_PP. The documentation of Text::CSV hints at maybe using ->is_quoted or the metadata information, but at least the implementation in Text::CSV_PP does not so any conditional quoting.

        That wasn't too difficult. See this commit. It will be available in version Text::CSV_XS version 1.18, but you can get it from github already.

        $ perl -Mblib -MText::CSV_XS \ ? -E'my $csv = Text::CSV_XS->new ({ quote_empty => 1 });' \ ? -E'$csv->combine (1, undef, "", " ", 2);' \ ? -E'say $csv->string' 1,,""," ",2

        update I forgot I already implemented this in the perl6 version Text::CSV.


        Enjoy, Have FUN! H.Merijn

        Your original post did not specify that. Please see I know what I mean. Why don't you? and How do I post a question effectively? Could you please show some sample input and output that is representative of what you are trying to print? Otherwise, we're just guessing at the correct answer.

        use Text::CSV; my $csv = Text::CSV->new({binary=>1,eol=>$/, quote_char=>undef,escape_char=>undef}); $csv->print(\*STDOUT, ["1","2",'""']); __END__ 1,2,""

        WHY? Why can 1 and 2 not be quoted? I could add quote_empty => 1 to Text::CSV_XS, but you've shown me no good reason so far.

        "1","2","" is just as valid CSV as 1,2,"" and 1,2, are and on parsing they'll all yield the same data (unless parsing options cause different rules).


        Enjoy, Have FUN! H.Merijn