How to read csv file with multi line in one field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ywang123
    New Member
    • May 2010
    • 2

    How to read csv file with multi line in one field

    I have a test.csv file with multi line in one field like following:

    Name, message
    "Gus", "See you"
    "Amy", "take to you later,
    Thank you.
    call me"

    "Mark", "Try it again

    Okay"


    Here is my code:
    Code:
    open(CSV, "test.csv") or die "Cannot open test.csv for read\n";
     my $string = <CSV>;
    
     while (<CSV>) 
     {
             chomp;
             my @list = split (",");
             foreach @list;
             print "$list[0] <$list[1]>\n";
    
     }
    It read first record right, but not the second and third ones. Can someone give me some advice how to read this csv file?

    Thanks a lot!!!!
    Last edited by numberwhun; May 19 '10, 04:50 PM. Reason: Please use CODE TAGS!
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You shouldn't use "chomp" as your first command, or you will delete the needed newline-character inside your message.
    Step 1.) get the next line
    Step 2.) Count the number of double quotation marks inside this line
    Step 3.) If there are four, you are done with the current record and can split by comma. Then chop and trim the double quotation marks on both ends. Process the resulting record data and go to step 1.
    If there are less than four, you should not trim or chop or process. Instead, read the next line and concatenate it with the old. Then go to step 2.
    If there are more than four, you should print out an error (too many columns) and exit the loop.

    I am not sure if there could be a double quotation mark inside the message, probably escaped by a backslash or so. If so, then you need to count only the the double quotation marks without that slash in front.

    Comment

    Working...