applescript to parse csv

Discussion in 'Mac Programming' started by SouthernAtHeart, Jan 8, 2012.

  1. macrumors newbie

    Joined:
    Nov 6, 2011
    #1
    I've spent quite a bit of time reading & searching, and it seems there's lots of talk of this, but I can't put my cursor on it.
    I need a script that will ask for a a CSV file and parse it out. The data will have quoted and unquoted values, embedded commas, and newlinefeed in quotes.
    The more I search, the more I'm learning that my original plan of writing the script myself is way beyond my scope, so I'm hoping to find someone that has done this already...
     
  2. macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    Explain what exactly it is you want to do with your csv file and what the output should be. Ideally post one row from it. awk makes it easy to work with csv files btw.
     
  3. thread starter macrumors newbie

    Joined:
    Nov 6, 2011
    #3
    Example with 6 fields
    One, 2, "three", "4, five", "and a
    Carriage return", 123-456-7890
     
  4. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #4
    Does it have to be an applescript? sed + awk + bash are great for these tasks. Perl is good too. Also, WTH do you want to accomplish?
     
  5. macrumors 68040

    Joined:
    Feb 2, 2008
    #5
    Yes, but what do you want to do with these fields? Are you interested in all rows but only specific columns, which? What output format? CSV?
     
  6. thread starter macrumors newbie

    Joined:
    Nov 6, 2011
    #6
    Yes, all the data.
    It's for importing contact data. I don't think I'll bother if it can't be done in AppleScript.
     
  7. macrumors regular

    Joined:
    Apr 11, 2003
    #7
    A quick Google search of "applescript explode" should do the trick. I took a glance at some of the results and found this one to be pretty good: http://applescript.bratis-lover.net/library/string/#explode

    "Explode" or "split" are common text parsing verbs (and functions/methods in programming languages) used for what you are wanting to do. Pick your delimiter (in your case ",") and a line from your file. It will create an array, with each index containing the value from each "column". Do what you will with the array. Then, move on to the next line. Repeat until EOF.

    Be careful, though. You say you are using contact information. You might find a comma inside one of your "columns" and I'm not sure how AppleScript will handle it. A lot of times a .csv will wrap text in double-quotes, so that may help.

    Good luck!
     
  8. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #8
    You need to define your input and your expected output. Otherwise all we can do is vaguely direct you to resources we think you might need.
     

Share This Page