Parse CSV file

Discussion in 'Perl Misc' started by mmittiga17@gmail.com, Sep 20, 2006.

  1. Guest

    Need help folks.

    I have a csv file that has some fields containing "," as part of the
    field and not a delimiter. These fields are double quoted.

    What I need to do is read each line, if the value in field 22 = X dont
    print
    $line. How can I get the value of each line's field 22, when split
    does not work on csv files that have quoted fields?

    example: "Potsdam, NY"

    This one is driving me crazy.

    I have only gotten about this far:

    use Text::parseWords;

    while (@ARGV) {
    $file=shift (@ARGV);
    open (IN,"$file") unless ($file =~ /\.Z/);
    while (defined($line = <IN>)) {
    @fields =quotewords(',', 0, $line);
    print "@fields[22]\n";

    }
    }
    I have also tried the regex for csv files from Mastering Regex.

    One thought was use a hash...

    Any help or suggestions would be appreciated.

    Thanks
     
    , Sep 20, 2006
    #1
    1. Advertising

  2. David Squire Guest

    wrote:
    > Need help folks.
    >
    > I have a csv file that has some fields containing "," as part of the
    > field and not a delimiter. These fields are double quoted.
    >
    > What I need to do is read each line, if the value in field 22 = X dont
    > print
    > $line. How can I get the value of each line's field 22, when split
    > does not work on csv files that have quoted fields?
    >
    > example: "Potsdam, NY"
    >
    > This one is driving me crazy.
    >
    > I have only gotten about this far:
    >
    > use Text::parseWords;
    >


    Use the right module for the job. Go to CPAN and search for CSV... or
    even "CSV simple" :)


    DS
     
    David Squire, Sep 20, 2006
    #2
    1. Advertising

  3. Xicheng Jia Guest

    wrote:
    > Need help folks.
    >
    > I have a csv file that has some fields containing "," as part of the
    > field and not a delimiter. These fields are double quoted.
    >
    > What I need to do is read each line, if the value in field 22 = X dont
    > print
    > $line. How can I get the value of each line's field 22, when split
    > does not work on csv files that have quoted fields?
    >
    > example: "Potsdam, NY"
    >
    > This one is driving me crazy.
    >
    > I have only gotten about this far:
    >
    > use Text::parseWords;
    >
    > while (@ARGV) {
    > $file=shift (@ARGV);
    > open (IN,"$file") unless ($file =~ /\.Z/);
    > while (defined($line = <IN>)) {
    > @fields =quotewords(',', 0, $line);


    how about(assumed no escaped double-quotes within a field):

    @fields = ($line =~ /"[^"]*"|[^",]+/g);
    $fields[21] =~ tr/"//d;
    print "$fields[21]\n";

    Xicheng
     
    Xicheng Jia, Sep 20, 2006
    #3
  4. Paul Lalli Guest

    Paul Lalli, Sep 20, 2006
    #4
    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. Tintin92
    Replies:
    1
    Views:
    1,779
    Andrew Thompson
    Feb 14, 2007
  2. jliu66
    Replies:
    0
    Views:
    542
    jliu66
    Oct 19, 2007
  3. sso
    Replies:
    20
    Views:
    2,747
    Martin Gregorie
    Apr 26, 2009
  4. Li Chen
    Replies:
    18
    Views:
    701
    Azmi Farih
    Mar 23, 2010
  5. Zhen Zhang

    parse a csv file into a text file

    Zhen Zhang, Feb 6, 2014, in forum: Python
    Replies:
    29
    Views:
    152
    Tim Chase
    Feb 6, 2014
Loading...

Share This Page