CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Parsing a CSV file with Snail::CSV

 



perl_rocks
Novice

Jan 29, 2009, 9:33 AM

Post #1 of 10 (1786 views)
Parsing a CSV file with Snail::CSV Can't Post

Hi All

I am trying to parse a file using Snail::CSV


Code
use Snail::CSV; 
use strict;

use Snail::CSV;
use Data::Dumper;

my $csv = Snail::CSV->new();

$csv->setFile("sample.csv", [ "Name", "Age", "Year" ]);


my $parsed = $csv->parse;

print Dumper($parsed);

$parsed is not what i expected.Someone knows how does this works.

Also do i have to use a while loop to read the whole file ?

Thanks in advance


KevinR
Veteran


Jan 29, 2009, 9:59 AM

Post #2 of 10 (1784 views)
Re: [perl_rocks] Parsing a CSV file with Snail::CSV [In reply to] Can't Post

I have never used the module but your usage looks correct. Since we have no idea what you expect its impossible to know what the problem is, if any.
-------------------------------------------------


perl_rocks
Novice

Jan 29, 2009, 10:07 AM

Post #3 of 10 (1783 views)
Re: [KevinR] Parsing a CSV file with Snail::CSV [In reply to] Can't Post

Hi

Thanks for the response

I am pasting the output and attaching the sample csv file


C:\Perl\Parse_Snail>perl Parse.pl
$VAR1 = [
{
'NUMBER' => 1,
'Name' => '╨╧◄αí▒'
}
];

C:\Perl\Parse_Snail>
Attachments: Sample.csv (13.5 KB)


FishMonger
Veteran / Moderator

Jan 29, 2009, 10:33 AM

Post #4 of 10 (1776 views)
Re: [perl_rocks] Parsing a CSV file with Snail::CSV [In reply to] Can't Post

That's a binary excel file, not a plain text csv file.

You're using the wrong module to parse that file.

Try:
Spreadsheet::ParseExcel
http://search.cpan.org/~jmcnamara/Spreadsheet-ParseExcel-0.49/lib/Spreadsheet/ParseExcel.pm

or:
Spreadsheet::ParseExcel::Simple
http://search.cpan.org/~tmtm/Spreadsheet-ParseExcel-Simple-1.04/lib/Spreadsheet/ParseExcel/Simple.pm

or, save the file as a plain text csv file and then try your current script.


perl_rocks
Novice

Jan 29, 2009, 10:37 AM

Post #5 of 10 (1773 views)
Re: [FishMonger] Parsing a CSV file with Snail::CSV [In reply to] Can't Post

I am a little confused

The file is a .csv file and I thought Snail is for :
Snail::CSV - Perl extension for read/write/update CSV files.


Sorry if this is basic stuff

When you say plain test csv file you mean a .txt ?

Thanks


FishMonger
Veteran / Moderator

Jan 29, 2009, 10:47 AM

Post #6 of 10 (1772 views)
Re: [perl_rocks] Parsing a CSV file with Snail::CSV [In reply to] Can't Post

Just because it has a .csv extension doesn't mean that it's a text file. That goes for a file with a .txt extension as well, but it's rare for it not to be plain text.. It's the contents of the file that matters.


perl_rocks
Novice

Jan 29, 2009, 12:30 PM

Post #7 of 10 (1767 views)
Re: [FishMonger] Parsing a CSV file with Snail::CSV [In reply to] Can't Post

Totally agree

But when i look at the type of file it says "Microsoft Office Excel Comma Separated Values File".Inst that what a csv file is

Thanks


FishMonger
Veteran / Moderator

Jan 29, 2009, 12:42 PM

Post #8 of 10 (1766 views)
Re: [perl_rocks] Parsing a CSV file with Snail::CSV [In reply to] Can't Post

"Microsoft Office Excel ...."

That should give you a clue that it's a proprietary format.

Open the file in notepad or similar text editor and you'll see that it's not a plain text file.


KevinR
Veteran


Jan 29, 2009, 12:53 PM

Post #9 of 10 (1763 views)
Re: [perl_rocks] Parsing a CSV file with Snail::CSV [In reply to] Can't Post

Its unfortunately true. The Ecel file you attached is not a text file, its binary and you will have to use a program that knows how to read that type of file and convert it into human readable text. If you stick with perl you can use one of the modules Fish listed or search for a Win32 module if you are using perl on Windows.
-------------------------------------------------


perl_rocks
Novice

Jan 29, 2009, 1:03 PM

Post #10 of 10 (1761 views)
Re: [KevinR] Parsing a CSV file with Snail::CSV [In reply to] Can't Post

Got it

Thanks for your help guys

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives