Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How check whether csv file with header is empty file or not in perl

by hi123 (Initiate)
on Sep 06, 2012 at 09:49 UTC ( #992048=perlquestion: print w/ replies, xml ) Need Help??
hi123 has asked for the wisdom of the Perl Monks concerning the following question:


I have a csv file xyz.csv with contents

name,age,place,phone
xyz,12,ohio,7372829
sed,23,hrh,9890908
I need to parse the csv file and check whether it is a empty file ie it doesnt have the values for the headers provided

a null file xyz.csv would contain just the headers (headers no may decrease or increase) eg :

name,age,place,phone
name,age,place,mob,phno,ht
how do i check for a null file in below code and print whtr null or not?
i have developed below script to parse the csv
open(my $data, '<', $file_c) or die "Could not open '$file_c' $!\n +"; while (my $line = <$data>) { next if ($. == 1); chomp $line; my @fields = split "," , $line; print"$fields[0] fields[1]"; }
please advise...

Comment on How check whether csv file with header is empty file or not in perl
Download Code
Re: How check whether csv file with header is empty file or not in perl
by CountZero (Bishop) on Sep 06, 2012 at 10:19 UTC
    Try this:
    use Modern::Perl; say 'Empty file' if 1 == scalar grep {/[^\s,]/} <DATA>; __DATA__ header1, header2, header3 ,,,,,,, , , , , , ,
    This will say "Empty file" if the file only contains a header line plus optionally empty lines or lines with nothing but whitespace and comma's.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Hi...where should i include ur line in my script,pls advise
        Here it is:
        use Modern::Perl; open(my $data, '<', $file_c) or die "Could not open '$file_c' $!"; say 'Empty file' if 1 == scalar grep {/[^\s,]/} <$data>;

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
Re: How check whether csv file with header is empty file or not in perl
by bart (Canon) on Sep 06, 2012 at 10:26 UTC
    Let me edit your code...
    open(my $data, '<', $file_c) or die "Could not open '$file_c' $!\n"; my $rows = 0; while (my $line = <$data>) { next if ($. == 1); chomp $line; my @fields = split "," , $line or next; $rows++; print"$fields[0] fields[1]"; } print "Total: $rows rows.\n" if $rows; print "No rows were found.n" unless $rows;
      Thanks bart!!!!

      Let me edit your code, which is very unsafe once the CSV gets a little bit more complicated ...

      use Text::CSV_XS; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 }); open my $data, "<", $file_c or die "$file_c: $!\n"; my $rows = 0; while (my $row = $csv->getline ($data)) { $rows++; } print "Total $rows rows (including header line)\n";

      Though I'd probably go to an even simpler solution using DBD::CSV:

      $ ll xx.csv 12862169 -rw-rw-rw- 1 merijn users 415423 Sep 6 16:00 xx.csv $ perl -MDBI -wE'say DBI->connect("dbi:CSV:f_ext=.csv")->selectrow_arr +ayref("select count(*) from xx")->[0]," rows in fs"' 8739 rows in fs

      Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-12-26 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls