Re: Importing/parsing CSV files
Re: Importing/parsing CSV files
- Subject: Re: Importing/parsing CSV files
- From: T&B <email@hidden>
- Date: Wed, 13 Sep 2006 10:45:37 +1000
As Yvan pointed out (thanks), I neglected to paste in the comma definition in my previous code posting, so here it is again, for completeness.
I welcome any bug reports or tweaks which speed it up.
You're welcome to use the script, just please keep my URL comment in it.
Thanks,
Tom
T&B
property comma: ","
property quot : "\"" -- Because earlier Mac OS doesn't know quote constant
property linefeed : ASCII character 10
on CsvToList(csvText)
-- 2006.09.12 T&B http://www.tandb.com.au/applescript/
-- Method: stepping through each character in csvText
set queueText to ""
set queueRow to {}
set queueTable to {}
set inQuotes to false
set previousChar to ""
set csvTextLength to length of csvText
repeat with charN from 1 to csvTextLength
set thisChar to character charN in csvText
if thisChar is quot then
if not inQuotes and previousChar is quot then
-- double quote within quotes so actually use a quote
set queueText to queueText & thisChar
end if
set inQuotes to not inQuotes
else if inQuotes then
set queueText to queueText & thisChar
else if thisChar is comma then
set end in queueRow to queueText
set queueText to ""
else if (thisChar is return or thisChar is linefeed) and not inQuotes then
if previousChar is return and thisChar is linefeed then
-- do nothing since new record already created
else
set end in queueRow to queueText
set queueText to ""
set end in queueTable to {} & queueRow
set queueRow to {}
end if
else if charN is csvTextLength then
set queueText to queueText & thisChar
set end in queueRow to queueText
set end in queueTable to {} & queueRow
else
set queueText to queueText & thisChar
end if
set previousChar to thisChar
end repeat
return queueTable
end CsvToList
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden