Using Text::CSV_XS perl module for parsing CSV with newlines -


we using text::csv_xs module parsing csv following options:

my $csv = text::csv_xs->new ({                                binary=> 1, eol => "\n",                                allow_loose_quotes => 1,                                allow_loose_escapes=> 1,                               escape_char => "\\"                              }); 

the csv created using mysql load data infile. if there null values mysql adds \n in fields

before parsing data :

1973127,99,\n 

if use escape_char => "\\" \n replaced n

data after parsing:

1973127,99,n 

also, data double '\' removed

eg: d\\'mello changed d'mello

if remove escape_char => "\\" data changed shown below

before parsing :

1539190,125,"\" shiddh - shila \"\ \"126" 

after parsing:

1539190,125,\\" shiddh - shila \\"\\n\\"126 

what best way resolve issue?

you can clean csv file first.

#!/usr/bin/perl  use strict; use warnings;  $file = shift; open csv, "<$file" or die; foreach $line (<csv>) {     $line =~ s/\\n/null/g;     print $line; } close csv; 

perl cleanup.pl my.csv > new.csv


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -