in reply to Writing to csv file

This might be bad practice. I don't know; a fellow monk will have to tell me if it is.

When I don't know the application that will be reading my csv file, I search for commas before writing to the file and replace it with a random character that means nothing. For instances/,/|/g. This way the reader of the csv file can always split by commas. The only thing to look out for is which character you pick to use to replace the comma.

Replies are listed 'Best First'.
Re^2: Writing to csv file
by johngg (Canon) on Mar 02, 2011 at 23:58 UTC

    The problem with replacing a comma with a different character, such as a pipe symbol, is that there is a good chance that that character will turn up in the data at some point in the future. If taking this approach I think I would lengthen the odds by using a longer string rather than a single character, something like

    foreach my $field ( @fields ) { $field =~ s{,}{::comma::}g; }

    I hope this is of interest.

    Cheers,

    JohnGG

Re^2: Writing to csv file
by Marshall (Canon) on Mar 02, 2011 at 22:41 UTC
    I do not substitute a comma in the text with another character. I just normally use "|" to separate fields. See post below.