Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Text::CSV_XS and encoding

by PeterKaagman (Sexton)
on Sep 17, 2018 at 21:29 UTC ( [id://1222538]=note: print w/replies, xml ) Need Help??


in reply to Text::CSV_XS and encoding

Just to let you all know

  • Funny escape sequence at the start of my stream is gone
  • Special chars appear nicely in an output file (assuming it will allso work for the database

This monk has found some rest in this monastary.... thank you all _o_

Putting it all together:

my @medewerkers; open(my $FH, '<:encoding(utf8)', \$res->content) || die("could not + open result as file: $!"); my $csv = Text::CSV->new({ sep_char => ';', binary => 1, auto_diag => 1}); $csv->header($FH,{detect_bom => 1}); while (my $row = $csv->getline_hr($FH) ){ push @medewerkers, $row; } close $FH; return \@medewerkers;

Replies are listed 'Best First'.
Re^2: Text::CSV_XS and encoding
by Tux (Canon) on Sep 18, 2018 at 06:49 UTC

    As Text::CSV is back in sync with Text::CSV_XS, you can shorten that a lot :)

    #!/usr/bin/env perl use 5.14.2; use warnings; use Text::CSV "csv"; use Data::Peek; my $content = <<"EOC"; id;naaam;datum in dienst;functie 1;Jip;2001-04-14;Chef lege dozen 2;Janneke;2013-10-01;Miep kraak EOC my @medewerkers; open my $FH, "<:encoding(utf8)", \$content or die "could not open result as file-handle: $!"; my $csv = Text::CSV_XS->new ({ sep_char => ";", binary => 1, auto_diag => 1, }); $csv->header ($FH, { detect_bom => 1 }); while (my $row = $csv->getline_hr ($FH)) { push @medewerkers, $row; } close $FH; DDumper \@medewerkers;
    [ { 'datum in dienst' => '2001-04-14', functie => 'Chef lege dozen', id => '1', naaam => 'Jip' }, { 'datum in dienst' => '2013-10-01', functie => 'Miep kraak', id => '2', naaam => 'Janneke' } ]

    Now shorten that to

    #!pro/bin/perl use 5.14.2; use warnings; use Text::CSV "csv"; use Data::Peek; my $content = <<"EOC"; id;naaam;datum in dienst;functie 1;Jip;2001-04-14;Chef lege dozen 2;Janneke;2013-10-01;Miep kraak EOC DDumper csv (in => \$content, bom => 1);

    Or, if you want to be more explicit

    csv (in => \$content, bom => 1, sep => ";");

    Which would reduce your function to

    sub csv_content { my $res = shift; return csv (in => \$res->content, bom => 1, sep => ";"); } # csv_content

    Add some error handling and you're done :)


    Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-23 09:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found