Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Reading slective columns from *.csv file

Reply
Thread Tools

Reading slective columns from *.csv file

 
 
Nithin
Guest
Posts: n/a
 
      12-08-2003
Hi,
I have a *.csv file that looks like this. I need to ignore Rows 1-5
and start reading from Row 6. From Row 6 I need only Column1, Column
3, Column4 and so on....(has about 35 values in each row).


RED - Letters in Red auto-populate columns from left to right.
Color Code - Yellow Field name boxes in yellow are fields required
Color Code - Gray Field name boxes in gray are fields are optional
Color Code - Green Field name boxes in green are optional fields
Color Code - Orange Field name boxes in orange indicate d - refer etc
44383 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT
44384 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT
44385 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT


Previously, I used to copy manually the values I wanted to the
input.csv file. But, now I want to read directly from the source file
and avoid the extra step.

My code looks like this

open FILE, "input.csv";

while (<FILE>) {
chomp;
my ($col1, $col2, $col3, $col4, $col6) = split (/\,/);
}
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-08-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Nithin) wrote in
news:(E-Mail Removed) m:

> Hi,
> I have a *.csv file that looks like this. I need to ignore Rows 1-5
> and start reading from Row 6. From Row 6 I need only Column1, Column
> 3, Column4 and so on....(has about 35 values in each row).


So, skip the first 5 lines.

> RED - Letters in Red auto-populate columns from left to right.
> Color Code - Yellow Field name boxes in yellow are fields required
> Color Code - Gray Field name boxes in gray are fields are optional
> Color Code - Green Field name boxes in green are optional fields
> Color Code - Orange Field name boxes in orange indicate d - refer etc
> 44383 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT
> 44384 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT
> 44385 Ontario Dork Canal 288 1398 Malheur Ave
> TAC31NT


Is this your data file? It is not in CSV format.

> My code looks like this
>
> open FILE, "input.csv";


Always check if open succeeded.

open FILE, '<', 'input.csv' or die "Cannot open input: $!\n";

> while (<FILE>) {
> chomp;
> my ($col1, $col2, $col3, $col4, $col6) = split (/\,/);
> }


So, basically, you haven't done anything and you'd like someone else to
do all the work for you. I would have thought it went without saying that
so should at least show some code that tries to accomplish your goal of
skipping the first few lines. Very annoying.

> my ($col1, $col2, $col3, $col4, $col6) = split (/\,/);


Have you looked at Text::CSV? It would help you avoid all sorts of
troubles that would be cause by this kind of code.

Untested:

use strict;
use warnings;

open FILE, '<', 'input.csv' or die "Cannot open input: $!\n";

for (1..5) {
unless(defined <FILE>) {
die "Error skipping header: $!\n";
}
}

use Text::CSV;
my $csv = Text::CSV->new();
while(<FILE>) {
if($csv->parse($_)) {
for ( $csv->fields() ) {
print;
}
print "\n";
}
}
__END__



--
A. Sinan Unur
(E-Mail Removed)
Remove dashes for address
Spam bait: (E-Mail Removed)
 
Reply With Quote
 
 
 
 
James Willmore
Guest
Posts: n/a
 
      12-08-2003
On 8 Dec 2003 10:59:20 -0800
(E-Mail Removed) (Nithin) wrote:

> Hi,
> I have a *.csv file that looks like this. I need to ignore Rows 1-5
> and start reading from Row 6. From Row 6 I need only Column1, Column
> 3, Column4 and so on....(has about 35 values in each row).
>
>
> RED - Letters in Red auto-populate columns from left to right.
> Color Code - Yellow Field name boxes in yellow are fields required
> Color Code - Gray Field name boxes in gray are fields are optional
> Color Code - Green Field name boxes in green are optional fields
> Color Code - Orange Field name boxes in orange indicate d - refer
> etc 44383 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT
> 44384 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT
> 44385 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT
>
>
> Previously, I used to copy manually the values I wanted to the
> input.csv file. But, now I want to read directly from the source
> file and avoid the extra step.
>
> My code looks like this
>
> open FILE, "input.csv";


open FILE, 'input.csv' or die "Can't open input.csv file: $!\n;


> while (<FILE>) {


next if $. <= 5;

> chomp;
> my ($col1, $col2, $col3, $col4, $col6) = split (/\,/);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^
Are you *sure* you want to split on ','? I didn't see a comma in the
data provided.

> }



There are other ways. But this is what I came up with in about 30
seconds (untested)

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Bennett's Laws of Horticulture: (1) Houses are for people to
live in. (2) Gardens are for plants to live in. (3) There is
no such thing as a houseplant.
 
Reply With Quote
 
Eric J. Roode
Guest
Posts: n/a
 
      12-08-2003
(E-Mail Removed) (Nithin) wrote in
news:(E-Mail Removed) m:

> Hi,
> I have a *.csv file that looks like this. I need to ignore Rows 1-5
> and start reading from Row 6. From Row 6 I need only Column1, Column
> 3, Column4 and so on....(has about 35 values in each row).

....
> Previously, I used to copy manually the values I wanted to the
> input.csv file. But, now I want to read directly from the source file
> and avoid the extra step.
>
> My code looks like this
>
> open FILE, "input.csv";
>
> while (<FILE>) {
> chomp;
> my ($col1, $col2, $col3, $col4, $col6) = split (/\,/);
> }


Did you have a question in there?

--
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print
 
Reply With Quote
 
Sara
Guest
Posts: n/a
 
      12-09-2003
(E-Mail Removed) (Nithin) wrote in message news:<(E-Mail Removed) om>...
> Hi,
> I have a *.csv file that looks like this. I need to ignore Rows 1-5
> and start reading from Row 6. From Row 6 I need only Column1, Column
> 3, Column4 and so on....(has about 35 values in each row).
>
>
> RED - Letters in Red auto-populate columns from left to right.
> Color Code - Yellow Field name boxes in yellow are fields required
> Color Code - Gray Field name boxes in gray are fields are optional
> Color Code - Green Field name boxes in green are optional fields
> Color Code - Orange Field name boxes in orange indicate d - refer etc
> 44383 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT
> 44384 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT
> 44385 Ontario Dork Canal 288 1398 Malheur Ave TAC31NT
>
>
> Previously, I used to copy manually the values I wanted to the
> input.csv file. But, now I want to read directly from the source file
> and avoid the extra step.
>
> My code looks like this
>
> open FILE, "input.csv";
>
> while (<FILE>) {
> chomp;
> my ($col1, $col2, $col3, $col4, $col6) = split (/\,/);
> }



a few comments:

1.) For a csv there seems to be a small number of commas. A csv file
should have a number of commas c where c > 0.

2.) No matter what language you're using, if you see something like

($col1, $col2, $col3, $col4, $col6)

this should STRONGLY suggest to you an ARRAY data structure. Much
better to use

my @cols = split /,/;


3.) If you can get the file into an array of lines, its useful for
debugging and other purposes as well. Its also best to use guerilla
tacxtics with files- get in, grab what you need, get out. Not
practical for all file sizes however.


use strict; # never leave home without it!

die "file cannot be opened for some odd reason: $!\n" unlesss open F,
"input.csv"; # do a little error checking

my @lines = <F>;
close F;

die "strange, nothing here!\n" unless @lines; # another error check

my @cols;
for (@lines)
{chomp; # you may not really need to chomp

# note only 1 arg since $_ is the default- clean syntax,
# also no need to \ the , it wont get misinterpreted in this context
@cols = split /,/;

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Align table columns with gridview columns Jacksm ASP .Net 2 11-21-2006 09:49 PM
Read CSV - string Columns - Int columns =?Utf-8?B?YmVub2l0?= ASP .Net 0 05-08-2006 02:11 PM
database columns vs. gui columns and sorting ittay.dror@gmail.com Java 5 03-04-2006 10:48 AM
CSS columns problem - faux columns don't work henrybranson@hotmail.com HTML 4 11-24-2005 10:05 AM
convert rows to columns and columns to rows helpful sql ASP .Net 0 05-19-2005 06:03 PM



Advertisments