Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

A module to parse CSVs line by line with an ability to set delimiter

by Doctrin (Beadle)
on Mar 26, 2013 at 14:14 UTC ( #1025528=perlquestion: print w/ replies, xml ) Need Help??
Doctrin has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear Monks. I have a stupid question. Is there a module to parse CSV line by line AND set a delimiter (it is ',' by default, but in my CSV file it is ';')? I tried this but did not find out how to set a delimiter for input file :( PS Example of a string of my CSV file: 163;508,1;0;25,41;53;00.01.00;0 ";" is supposed to be a delimiter, so that I wanna have following values: 163 508,1 0 25,41 53 00.01.00 0 . So, is there a module that reads CSV line by line AND an ability to set a user-defined delimiter? Thanks in advance

Comment on A module to parse CSVs line by line with an ability to set delimiter
Download Code
Re: A module to parse CSVs line by line with an ability to set delimiter
by hdb (Prior) on Mar 26, 2013 at 14:19 UTC

    Text::CSV supports a "sep_char" variable in its new() method which you can set to semicolon.

Re: A module to parse CSVs line by line with an ability to set delimiter
by marto (Chancellor) on Mar 26, 2013 at 14:19 UTC

    The Text::CSV documentation explains how to set the sep_char and provides example use e.g.

    my $csv = Text::CSV->new ({ sep_char => ';' });
Re: A module to parse CSVs line by line with an ability to set delimiter
by Tux (Monsignor) on Mar 26, 2013 at 14:23 UTC

    You tried Text::CSV, which is the wrapper over Text::CSV_pp (pure perl) and the fast Text::CSV_XS. They both have the same syntax:

    use Text::CSV; my $parser = Text::CSV->new ({ binary => 1, # allow binary data auto_diag => 1, # allow automatic warnings and errors sep_char => ",", # , is the default, ; is also use quite often }); open my $fh, "<", "file.csv" or die "file.csv: $!"; while (my $row = $csv->getline ($fh)) { say "The second field is ", $row->[1]; }

    Enjoy, Have FUN! H.Merijn
Re: A module to parse CSVs line by line with an ability to set delimiter
by 2teez (Priest) on Mar 26, 2013 at 14:24 UTC
    You can also check Text::CSV_XS module..
    update
    Tux ++ fast fingers.
    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: A module to parse CSVs line by line with an ability to set delimiter
by Tux (Monsignor) on Mar 26, 2013 at 14:27 UTC

    You tried Text::CSV, which is the wrapper over Text::CSV_PP (pure perl) and the fast Text::CSV_XS. They both have the same syntax:

    use Text::CSV; my $csv = Text::CSV->new ({ binary => 1, # allow binary data auto_diag => 1, # allow automatic warnings and errors sep_char => ",", # , is the default, ; is also use quite often }); open my $fh, "<", "file.csv" or die "file.csv: $!"; while (my $row = $csv->getline ($fh)) { say "The second field is ", $row->[1]; }

    If you want a parser WITHIN a parser, just create a second parser

    use Text::CSV; my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char => ",", }); my $parser = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char => ";", # sep is ; }); open my $fh, "<", "file.csv" or die "file.csv: $!"; while (my $row = $csv->getline ($fh)) { say "The second field is ", $row->[1]; foreach my $f (@$row) { $parser->parse ($f) or next; my @subfields = $parse->fields; } }

    Enjoy, Have FUN! H.Merijn
Re: A module to parse CSVs line by line with an ability to set delimiter
by punch_card_don (Curate) on Mar 26, 2013 at 15:06 UTC
    The DBI is your friend.

    DBD::CSV

    $dbh = DBI->connect (
    "dbi:CSV:f_dir=$ENV{HOME}/csvdb;f_ext=.csv;f_lock=2;" .
    "f_encoding=utf8;csv_eol=\n;csv_sep_char=\\;;"

    "...Pay attention to the semi-colon for csv_sep_char (as seen in many CSV exports from MS Excel) is being escaped in below example, as is would otherwise be seen as attribute separator:..."




    Time flies like an arrow. Fruit flies like a banana.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2014-09-18 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (108 votes), past polls