Reading slective columns from *.csv file

N

Nithin

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

A. Sinan Unur

(e-mail address removed) (Nithin) wrote in
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__
 
J

James Willmore

On 8 Dec 2003 10:59:20 -0800
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.
 
E

Eric J. Roode

(e-mail address removed) (Nithin) wrote in
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? :)
 
S

Sara

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 /,/;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top