PDA

View Full Version : applescript to parse csv




SouthernAtHeart
Jan 8, 2012, 10:09 PM
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...



subsonix
Jan 9, 2012, 12:20 AM
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.

SouthernAtHeart
Jan 9, 2012, 01:02 AM
Example with 6 fields
One, 2, "three", "4, five", "and a
Carriage return", 123-456-7890

mobilehaathi
Jan 9, 2012, 01:13 AM
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?

subsonix
Jan 9, 2012, 01:24 AM
Example with 6 fields
One, 2, "three", "4, five", "and a
Carriage return", 123-456-7890

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?

SouthernAtHeart
Jan 9, 2012, 07:34 AM
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?

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.

Thom_Edwards
Jan 9, 2012, 10:54 AM
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!

mobilehaathi
Jan 9, 2012, 11:01 AM
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.

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.