[ale] Parsing CSV file in perl

Jimmy Oliver jimmyoliver at gmail.com
Thu Jul 7 10:11:30 EDT 2005


On 7/6/05, Christopher Fowler <cfowler at outpostsentinel.com> wrote:
> I'm trying to parse a CSV file in perl and I'm having a issue with some
> of the columns being blank.
> 

I usually convert tab separated CSV's to actual comma delimited CSV's
then I use the perl split function.


> Here is a sample piece of data.
> 
> Id    LASTNAME    FIRSTNAME
>       Adams       Portia
> 10572 Alexander   Robert
> 
> You can see that the first row does not have an ID.  This can be true
> for all columns.  They may or may not have values.
> 
> Here is how I'm trying the parse:
> 
> open TXT, "< Expanded_2005_Select_1.csv";
> while(<TXT>) {
>         m/^(\d+?)\t/;
>         print "$1\n";
> }
> 
> Each columns is tab delimeted.  When I run this I get the lastname in $1
> for the first line and the the ID in $1 for the second line.  I need to
> somehow create a regex that would be unforgiving of nothing being there.
> 
> Data file looks like this:
>       1 "ID"    "LASTNAME"      "FIRSTNAME"     "TITLE" "COMPANY"
> "ADDRESS        "       "ADDRESS2"      "CITY"  "STATE" "ZIPCODE"
> "COUNTRY"               "PHONE" "EMAIL" "REGTYPE"       "DATE"  "TIME"
> "Question1"     "Questio        n2"     "Question3"     "READERID"
>       2         "Adams" "Portia"        "Director"      "The Rockefeller
> Univers        ity"    "1230 York Ave "                "New York"
> "NY"    "10021-6        399"    "USA"   2123277719
> "adams at rockefeller.edu" "Member"
>       3 10572   "Alexander"     "Robert"        "Manager Voice & Video
> Solution"                "Air Products and Chemicals, Inc"       "7201
> Hamilton Blvd"                    "Allentown"     "PA"    "18195-1501"
> "USA"   "610-481-7156"          "alexanrw at airproducts.com"      "Member"
> 06/12/2005      06:06:14         pm                             60711
> 
> The 1,2,3 that you see is the line numbers in VI
> 
> 
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale
> 


-- 
________________________
Jimmy Oliver
http://www.jimmyoliver.net
email: jimmy at jimmyoliver.net
lists:  jimmyoliver at gmail.com



More information about the Ale mailing list