in reply to Re: problem with csv parsing
in thread problem with csv parsing

I added callback after the handler initialisation:
$CSV_H->callbacks(error=>\&onerror);
and added a function:
sub onerror{ print '=' x 100, "\n"; printf "ERROR_INPUT: %s\n", $CSV_H->error_input(); printf "EOF: %s\n", ($CSV_H->eof)?('EOF'):('ERROR'); }
, but it doesn't seem to work. None of the print statements from the sub have put anything out to the screen. There was no output.

Replies are listed 'Best First'.
Re^3: problem with csv parsing
by Tux (Canon) on May 21, 2020 at 08:46 UTC

    That is the hard way to get neat errors. Just add auto_diag => 2

    Another approach might be to use csv-check and get the options right before you go for the real thing:

    $ csv-check -v1 test.ssv
    Checked test.ssv with csv-check 2.05
    using Text::CSV_XS 1.42 with perl 5.30.0 and Unicode 12.1.0
    test.ssv record 1 at line 1/104 - 2034 - EIF - Loose unescaped quote
        |147;lakjfh lkjsfh ehjd;134-324-730 31;291;24.04.2020;15 000,00;severo-vostocnoe otdelenie \x{02116} 8645 pao "sberbank rossii";4243972345;347636334;23452347344633423542;severo-vostocnoe otdelenie N8645 pao sberbank g. magadan;896868986;98375423895239529987;;96764128476876487264\n|
        |                                                                                                             ▲                                                                                                                                                                          |
    # CSV_XS ERROR: 2034 - EIF - Loose unescaped quote @ rec 0 pos 104 field 2
    
    $ csv-check -v1 test.ssv --allow-loose-quotes
    Checked test.ssv with csv-check 2.05
    using Text::CSV_XS 1.42 with perl 5.30.0 and Unicode 12.1.0
    OK: rows: 16, columns: 2
        sep = <,>, quo = <">, bin = <1>, eol = <"\n">
    

    Once you get the options so that you are able to parse your (invalid) data, show the used attributes with -L:

    $ csv-check -v1 test.ssv --allow-loose-quotes -L allow_loose_escapes : 0 allow_loose_quotes : 1 allow_unquoted_escape : 0 allow_whitespace : 0 always_quote : 0 auto_diag : 1 binary : 1 blank_is_undef : 0 callbacks : (undef) decode_utf8 : 1 diag_verbose : 0 empty_is_undef : 0 eol : escape_char : " escape_null : 1 formula : diag keep_meta_info : 1 quote : (undef) quote_binary : 1 quote_char : " quote_empty : 0 quote_space : 1 sep : (undef) sep_char : strict : 0 types : (undef) undef_str : (undef) verbatim : 0

    Or just show the options that changed the defaults (note that csv-check sets some sane attributes that are not default):

    $ csv-check -v1 test.ssv --allow-loose-quotes -X allow_loose_quotes : 1 auto_diag : 1 binary : 1 formula : diag keep_meta_info : 1

    So, your code would now be:

    use Text::CSV_XS; # Text::CSV_XS is much faster that Text::CSV my $fileTable = shift; my $CSV_H = Text::CSV_XS->new ({ sep_char => ";", binary => 1, blank_is_undef => 1, empty_is_undef => 1, allow_whitespace => 0, allow_loose_quotes => 1, # Should work. If not, maybe a bug in Tex +t::CSV auto_diag => 2, # Added });

    Enjoy, Have FUN! H.Merijn
Re^3: problem with csv parsing
by hippo (Archbishop) on May 21, 2020 at 08:15 UTC

    If your error callback isn't printing anything, that means that it isn't being called which means that there are no errors. Surely that's a good thing?

      As I've wrote in my original message, there are errors, just the error call back wasn't printing anything, but I've figure it out.
      I think, it was because I needed to configure auto_diag=>1.
      Now it works.