Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Text::CSV_XS separator character tab

by webchalkboard (Scribe)
on May 27, 2005 at 14:07 UTC ( [id://461067]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I've done a quick search and can't see any previous questions on this so I hope i'm not reasking old questions, but can anyone tell me how I can get Text::CSV_XS to use the tab (\t) as a delimiter? Define it as the separator character...?

This is my code for creating the object.

my $csv = Text::CSV_XS->new({ 'quote_char' => '', 'escape_char' => '', 'sep_char' => '\t', 'binary' => 1 });

But that doesnt seem to work... any ideas why? Do I need to escape it or something?

Thanks,

Tom

Learning without thought is labor lost; thought without learning is perilous. - Confucius
WebChalkboard.com | For the love of art...

Replies are listed 'Best First'.
Re: Text::CSV_XS separator character tab
by mpeters (Chaplain) on May 27, 2005 at 14:20 UTC
    You need to use doubled quotes ("\t") so that perl interpolates it as the tab char and not a literal string of '\t'.

    More people are killed every year by pigs than by sharks, which shows you how good we are at evaluating risk. -- Bruce Schneier
Re: Text::CSV_XS separator character tab
by reasonablekeith (Deacon) on May 27, 2005 at 14:20 UTC
    as a guess you want "\t" not '\t', which is the same as "\\t"
    ---
    my name's not Keith, and I'm not reasonable.
Re: Text::CSV_XS separator character tab
by ghenry (Vicar) on May 27, 2005 at 14:21 UTC

    Try 0x09

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!

      Tried that too... no luck... I put it in single and double quotes.

Re: Text::CSV_XS separator character tab
by dorward (Curate) on May 27, 2005 at 14:21 UTC
    Wild guess: Since single quotes around a string don't cause that string to be interpolated, wouldn't you want "\t" instead of '\t'?

      Thanks for the replies, but I tried

      my $csv = Text::CSV_XS->new({ 'quote_char' => '', 'escape_char' => '', 'sep_char' => "\t", 'binary' => 1 });

      But still no joy... the file i'm trying to split up is here: http://www.a4uforum.co.uk/images_a4u/misc/productsYourLenses.txt

        That file has null bytes in it, which seems to interfere with the Text::CSV_XS parser somehow. If you strip the null characters out of the file, it seems to work ok. (It does for me anyway.)

        use warnings; use strict; use Text::CSV_XS; use IO::File; my $file = 'productsYourLenses.txt'; my $fh = new IO::File; $fh->open("< $file") or die "Could not open $file"; my $csv = Text::CSV_XS->new({ 'quote_char' => '', 'escape_char' => '', 'sep_char' => "\t", 'binary' => 1 }); my $columns; while( 1 ){ $columns = $csv->getline($fh); last unless @$columns; print join '|', @$columns, "\n\n"; }

        Or, for something this simple you could just use split.

        use warnings; use strict; my $file = 'productsYourLenses.txt'; open my $fh, '<', $file or die "Could not open $file $!.\n"; my @rows; while(<$fh>){ chomp; s/\x00//g; my @columns = split /\t+/; push @rows, \@columns; } for (@rows){ print join '|', @$_, "\n\n"; }

        Both of these will yield an AoA.

        I was able to parse the file but since I have no idea what data to expect I don't know if it's parsing correctly. What do you mean by "no joy" - that's not very informative. Do you know how the file was produced? Are you sure it actually is a tab-separated file? What are the line endings, they show up as ^@ in emacs.
Re: Text::CSV_XS separator character tab
by kwaping (Priest) on May 27, 2005 at 16:21 UTC
    This is just a workaround to get it working in the short term, but you might try translating the file from TSV to CSV on the fly. Here's an example:
    # in your code, $text would be each line of the file inside a loop my $text = 'asfd asdf, fdsa asd "fds"asf 123'; $text =~ s/"/\\"/g; $text = qq|"$text"|; $text =~ s/\t/","/g; print "$text\n";
Re: HELP :: Text::CSV_XS separator character tab
by mlaro (Initiate) on Mar 03, 2012 at 17:11 UTC

    Hi All, I don't use perl very often and TEXT::CSV_XS is kicking my ass.

    I would use Text::CVS which was working great for me except that it doesn't handle french characters.

    I also have to use a different seperator because some field have multiple comma separated characters. This is the data:

    374|Mike who|kelticjan|mike@someemail.com|23/2/2012 10:57:32|0|0|Mike| +Clements|90 Street Ave|Halifax|New Brunswick|1t5 7y7|516-555-5555|lol + Transportation Inc.|a||a||||a,b,c,d,e,g,h,i,j,k||a|a|a|a|a,b,c,d,e|I +dle time / miles per gallon|a,b,c||b||a,d,f,g,h|

    this is what I am trying to do to parse it and print it out with no separator character.

    #!/usr/bin/perl use Text::CSV_XS; my $csv = Text::CSV_XS->new ({ quote_char => '"', escape_char => '"', sep_char => "|", eol => $\, always_quote => 0, quote_space => 1, quote_null => 1, quote_binary => 1, binary => 1, keep_meta_info => 0, allow_loose_quotes => 0, allow_loose_escapes => 0, allow_whitespace => 0, blank_is_undef => 0, empty_is_undef => 0, verbatim => 0, auto_diag => 0, }) or die "Cannot use CSV: ".Text::CSV_XS->error_diag (); open my $FH, "<:encoding(utf8)", "test.csv" or die "test.csv: $!" +; while (my $line = $csv->getline ($FH)){ if ($csv->parse (@$line)) { my @field = $csv->fields; foreach my $col (0 .. $#field) { print $field[$col], "\n" ; } } else { print STDERR "parse () failed on argument: ", $csv->error_input, "\n"; $csv->error_diag (); } }

    this is the output:

    374

    I've tried a bunch of ways and seem to be confused ... I'd appreciate some help.

    Thanks Mike

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found