neilwatson has asked for the wisdom of the Perl Monks concerning the following question:
Greetings, I'm trying to get Text::CSV's getline_hr_all to slurp an entire CSV into a data structure, but the structure appears empty. What is my mistake?
UPDATE: Allow_loose_quotes is the winner.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Text::CSV; my $file = $ARGV[0]; my $csv = Text::CSV->new ({ sep_char => '|', allow_whitespace => 1, binary => 1, eol => "|$/", allow_loose_quotes => 1, ## <--added for the win }); open my $csv_file, "<", $file or die "Cannot open [$file] [$!]"; my $headers_ref = $csv->getline($csv_file) or die "no header"; $csv->column_names($headers_ref); my $table_ref = $csv->getline_hr_all($csv_file); close $csv_file; print "headers: ".Dumper( $headers_ref ); print "table: ".Dumper( $table_ref );
A Some lines of the csv:
job_execution_id | job_status | step_name | job_params | job_instance +_id | read_count | write_count | exit_message | 625 | FAILED | parseSchemaStep | {"dataFeedURI":"\/storage\/D1200. +txt","metadataID":"meta.xml","Date":"2015-07-31","Id":"146"} | 625 | + 0 |0 | org.springframework.integration.transformer.MessageTransforma +tionException: ; nested exception is org.springframework.messaging.Me +ssageHandlingException: ; nested exception is java.io.IOException: IO +Exception encountered while retrieving metadata. at org.springframework.integration.transformer.MessageTransformingHand +ler.handleRequestMessage(MessageTransformingHandler.java:74) |
Now run it:
$ ./readjoblog.pl failedjob.csv headers: $VAR1 = [ 'job_execution_id', 'job_status', 'step_name', 'job_params', 'job_instance_id', 'read_count', 'write_count', 'exit_message' ]; table: $VAR1 = [];
Neil Watson
watson-wilson.ca
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Text::CSV and getline_hr_all
by Tux (Canon) on Feb 01, 2016 at 21:59 UTC | |
|
Re: Text::CSV and getline_hr_all
by poj (Abbot) on Feb 01, 2016 at 21:08 UTC |