FAQ
This isn't specifically a DBI question, but it is a part of a
database script I am writing. I am parsing a CSV file and populating a
MySQL database table. The problem I have run into is that a couple of
lines of data have a comma in a filed that is in quotes, such as "First
part, second part". This makes it difficult to parse the CSV file on
commas like my intention was. Could someone help me figure out how to
parse this? Thanks.
Basically, I am reading in the file in a 'while' loop. I am
then splitting the line into an array using the 'split' function. The
next step is to simply populate the database table with the fields.
Here is the 'split' I am currently using.

< ...snip... >
while (<DATA>) {
@data = split(/,\s*/);
print "$data[1]\n";
}
< ...snip... >

Thanks in advance for the help.

Scott Nipp
Phone: (214) 858-1289
E-mail: sn4265@sbc.com
Web: http:\\ldsa.sbcld.sbc.com

Search Discussions

  • Jeff Zucker at Sep 7, 2004 at 6:21 pm
    Is there some reason you aren't using DBD::CSV or its parser
    (Text::CSV_XS), which handle this and a number of other problem issues
    with parsing CSV? Why reinvent the wheel?

    --
    Jeff

    NIPP, SCOTT V (SBCSI) wrote:
    This isn't specifically a DBI question, but it is a part of a
    database script I am writing. I am parsing a CSV file and populating a
    MySQL database table. The problem I have run into is that a couple of
    lines of data have a comma in a filed that is in quotes, such as "First
    part, second part". This makes it difficult to parse the CSV file on
    commas like my intention was. Could someone help me figure out how to
    parse this? Thanks.
    Basically, I am reading in the file in a 'while' loop. I am
    then splitting the line into an array using the 'split' function. The
    next step is to simply populate the database table with the fields.
    Here is the 'split' I am currently using.

    < ...snip... >
    while (<DATA>) {
    @data = split(/,\s*/);
    print "$data[1]\n";
    }
    < ...snip... >

    Thanks in advance for the help.

    Scott Nipp
    Phone: (214) 858-1289
    E-mail: sn4265@sbc.com
    Web: http:\\ldsa.sbcld.sbc.com


  • NIPP, SCOTT V \(SBCSI\) at Sep 7, 2004 at 6:29 pm
    Actually, yes. There is a VERY good reason that I am not using
    this module...

    I had no idea that it existed. Now that I know, I'll take a
    peak and see if this will indeed do the trick for me. Thanks a lot for
    pointing me in the direction of the easy solution.

    Scott Nipp
    Phone: (214) 858-1289
    E-mail: sn4265@sbc.com
    Web: http:\\ldsa.sbcld.sbc.com



    -----Original Message-----
    From: Jeff Zucker
    Sent: Tuesday, September 07, 2004 1:18 PM
    To: NIPP, SCOTT V (SBCSI)
    Cc: dbi-users@perl.org
    Subject: Re: Parsing question...


    Is there some reason you aren't using DBD::CSV or its parser
    (Text::CSV_XS), which handle this and a number of other problem issues
    with parsing CSV? Why reinvent the wheel?

    --
    Jeff

    NIPP, SCOTT V (SBCSI) wrote:
    This isn't specifically a DBI question, but it is a part of a
    database script I am writing. I am parsing a CSV file and populating a
    MySQL database table. The problem I have run into is that a couple of
    lines of data have a comma in a filed that is in quotes, such as "First
    part, second part". This makes it difficult to parse the CSV file on
    commas like my intention was. Could someone help me figure out how to
    parse this? Thanks.
    Basically, I am reading in the file in a 'while' loop. I am
    then splitting the line into an array using the 'split' function. The
    next step is to simply populate the database table with the fields.
    Here is the 'split' I am currently using.

    < ...snip... >
    while (<DATA>) {
    @data = split(/,\s*/);
    print "$data[1]\n";
    }
    < ...snip... >

    Thanks in advance for the help.

    Scott Nipp
    Phone: (214) 858-1289
    E-mail: sn4265@sbc.com
    Web: http:\\ldsa.sbcld.sbc.com


  • Ravi Kongara at Sep 7, 2004 at 7:05 pm
    What i understood from your question is , you want to split strings that
    have comma within
    quotes. Forgive me if i'm wrong.

    If elements of @data are re-split , you can get multiple fields from it.

    < ...snip... >
    my @new_data;
    while (<DATA>) {
    @data = split(/,\s*/);
    foreach my $string ( @data ) {
    @temp = split /,\s*/, $string;
    push @new_data, @temp;
    }
    print "$data[1]\n";
    }
    < ...snip... >

    there may be better solutions, though, as i've never dealt with CSV files.

    regs,
    Ravi

    NIPP, SCOTT V (SBCSI) wrote:
    This isn't specifically a DBI question, but it is a part of a
    database script I am writing. I am parsing a CSV file and populating a
    MySQL database table. The problem I have run into is that a couple of
    lines of data have a comma in a filed that is in quotes, such as "First
    part, second part". This makes it difficult to parse the CSV file on
    commas like my intention was. Could someone help me figure out how to
    parse this? Thanks.
    Basically, I am reading in the file in a 'while' loop. I am
    then splitting the line into an array using the 'split' function. The
    next step is to simply populate the database table with the fields.
    Here is the 'split' I am currently using.

    < ...snip... >
    while (<DATA>) {
    @data = split(/,\s*/);
    print "$data[1]\n";
    }
    < ...snip... >

    Thanks in advance for the help.

    Scott Nipp
    Phone: (214) 858-1289
    E-mail: sn4265@sbc.com
    Web: http:\\ldsa.sbcld.sbc.com


  • NIPP, SCOTT V \(SBCSI\) at Sep 7, 2004 at 7:16 pm
    The suggestion of using Text::CSV_XS was the winner. This is a
    very simple module to use. Thanks for the feedback though.

    Scott Nipp
    Phone: (214) 858-1289
    E-mail: sn4265@sbc.com
    Web: http:\\ldsa.sbcld.sbc.com



    -----Original Message-----
    From: Ravi Kongara
    Sent: Tuesday, September 07, 2004 2:05 PM
    To: NIPP, SCOTT V (SBCSI)
    Cc: dbi-users@perl.org
    Subject: Re: Parsing question...


    What i understood from your question is , you want to split strings that

    have comma within
    quotes. Forgive me if i'm wrong.

    If elements of @data are re-split , you can get multiple fields from
    it.

    < ...snip... >
    my @new_data;
    while (<DATA>) {
    @data = split(/,\s*/);
    foreach my $string ( @data ) {
    @temp = split /,\s*/, $string;
    push @new_data, @temp;
    }
    print "$data[1]\n";
    }
    < ...snip... >

    there may be better solutions, though, as i've never dealt with CSV
    files.

    regs,
    Ravi

    NIPP, SCOTT V (SBCSI) wrote:
    This isn't specifically a DBI question, but it is a part of a
    database script I am writing. I am parsing a CSV file and populating a
    MySQL database table. The problem I have run into is that a couple of
    lines of data have a comma in a filed that is in quotes, such as "First
    part, second part". This makes it difficult to parse the CSV file on
    commas like my intention was. Could someone help me figure out how to
    parse this? Thanks.
    Basically, I am reading in the file in a 'while' loop. I am
    then splitting the line into an array using the 'split' function. The
    next step is to simply populate the database table with the fields.
    Here is the 'split' I am currently using.

    < ...snip... >
    while (<DATA>) {
    @data = split(/,\s*/);
    print "$data[1]\n";
    }
    < ...snip... >

    Thanks in advance for the help.

    Scott Nipp
    Phone: (214) 858-1289
    E-mail: sn4265@sbc.com
    Web: http:\\ldsa.sbcld.sbc.com


  • Clive Eisen at Sep 8, 2004 at 11:04 am
    Just goes to prove that one should always look on CPAN before writing
    almost anything ..........

    NIPP, SCOTT V (SBCSI) wrote:
    The suggestion of using Text::CSV_XS was the winner. This is a
    very simple module to use. Thanks for the feedback though.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbi-users @
categoriesperl
postedSep 7, '04 at 6:15p
activeSep 8, '04 at 11:04a
posts6
users4
websitedbi.perl.org

People

Translate

site design / logo © 2022 Grokbase