• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to parse CSV
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to parse CSV


  • Subject: Re: How to parse CSV
  • From: BareFeet <email@hidden>
  • Date: Wed, 23 Jan 2008 05:25:47 +1100

Hi Benedikt,

I'm looking for tutorials or other infos on how to parse a csv file into a list of lists in AppleScript.

A few methods spring to mind:

1. Pure AppleScript. I use the following handler.

(* CsvToListOfText 2008
BareFeet http://www.tandb.com.au
with a lot of help from Kai
t = the text to parse
delimiterString = the string to delimit items in a row, usually a comma
quoteString = the string to wrap around items containing delimiters, usually a double quote. null if no quotes (faster).
newLineString = the string to delimit the end of each line, usually a linefeed
*)

on CsvToListOfText(t, delimiterString, quoteString, newLineString)
if t is in {"", null} then
return t
else
set oldDelimiters to text item delimiters


if quoteString is null then
set text item delimiters to newLineString
else -- if quoteString is not null then


set quoteSubstitute to "<Qsub>"
set quote2Substitute to "<Q2sub>"
set newLineSubstitute to "<NLsub>"
set delimiterSubstitute to "<Dsub>"


repeat with findSubstitutePair in {{delimiterString & quoteString, delimiterString & quoteSubstitute}, {newLineString & quoteString, newLineString & quoteSubstitute}, {quoteString & quoteString, quote2Substitute}, {quoteSubstitute, quoteString}}
set {findString, substituteString} to findSubstitutePair
set text item delimiters to findString
set t to text items in t
set text item delimiters to substituteString
set t to t as string
end repeat


set text item delimiters to quoteString
end if


script listHandler
property l : t's text items
end script


if quoteString is null then
set text item delimiters to delimiterString
else
repeat with i from 1 to count listHandler's l by 2
set text item delimiters to delimiterString
set t to text items of listHandler's l's item i
set text item delimiters to delimiterSubstitute
set t to t as string


set text item delimiters to newLineString
set t to t's text items
set text item delimiters to newLineSubstitute
set listHandler's l's item i to t as string
end repeat
set text item delimiters to ""
set t to listHandler's l as string


set text item delimiters to quote2Substitute
set listHandler's l to t's text items
set text item delimiters to quoteString
set t to listHandler's l as string


set text item delimiters to newLineSubstitute
set listHandler's l to t's text items
set text item delimiters to delimiterSubstitute
end if -- quoteString is not null
repeat with i from 1 to count listHandler's l
set listHandler's l's item i to text items of listHandler's l's item i
end repeat
set text item delimiters to oldDelimiters
return listHandler's l
end if -- t is not in {"",null}
end CsvToListOfText

or:

2. Use Regex (a regular _expression_) to parse CSV. Pure AppleScript doesn't do regex, but you can get it to call something that does, such as Hai's TextCommand's or Satimage's scrioting additions, or use do shell to call perl or something else to invoke it. Here's a regex that's supposed to work, though I haven't tested calling it from AppleScript:

("(?:[^"]|"")*"|[^",\r\n]*)(,|\r\n?|\n)?


It's mentioned here: http://www.bennadel.com/blog/976-Regular-Expressions-Make-CSV-Parsing-In-ColdFusion-So-Much-Easier-And-Faster-.htm
and another here: http://snippets.dzone.com/posts/show/4430
and another here: http://dotnetslackers.com/Regex/re-7359_Regex_Very_simple_but_very_useful_regex_to_split_CSV_files_It_does_not_drop_the_commas_yo.aspx
and other places

or:

3. Script an app like Excel to open the CSV file and read the cell elements from the app.

or:

4. Call another faceless utility such as PHP or perl or ruby or python which may have a built in CSV handling routine. I only know of the "do shell script" method of invoking those, and have only tried with perl.

Tom
BareFeet (aka T&B)

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >How to parse CSV (From: Benedikt Quirmbach <email@hidden>)

  • Prev by Date: Re: How to parse CSV
  • Next by Date: Re: OT: Curl
  • Previous by thread: Re: How to parse CSV
  • Next by thread: Re: How to parse CSV
  • Index(es):
    • Date
    • Thread