Reading slective columns from *.csv file

Discussion in 'Perl Misc' started by Nithin, Dec 8, 2003.

  1. Nithin

    Nithin Guest

    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 (/\,/);
    }
     
    Nithin, Dec 8, 2003
    #1
    1. Advertising

  2. (Nithin) wrote in
    news::

    > 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

    Remove dashes for address
    Spam bait: mailto:
     
    A. Sinan Unur, Dec 8, 2003
    #2
    1. Advertising

  3. On 8 Dec 2003 10:59:20 -0800
    (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.
     
    James Willmore, Dec 8, 2003
    #3
  4. (Nithin) wrote in
    news::

    > 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
     
    Eric J. Roode, Dec 8, 2003
    #4
  5. Nithin

    Sara Guest

    (Nithin) wrote in message news:<>...
    > 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 /,/;
     
    Sara, Dec 9, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?YmVub2l0?=

    Read CSV - string Columns - Int columns

    =?Utf-8?B?YmVub2l0?=, May 8, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    443
    =?Utf-8?B?YmVub2l0?=
    May 8, 2006
  2. Tintin92
    Replies:
    1
    Views:
    1,752
    Andrew Thompson
    Feb 14, 2007
  3. Replies:
    7
    Views:
    2,553
    Roedy Green
    Jun 28, 2007
  4. jliu66
    Replies:
    0
    Views:
    528
    jliu66
    Oct 19, 2007
  5. Li Chen
    Replies:
    18
    Views:
    687
    Azmi Farih
    Mar 23, 2010
Loading...

Share This Page