Re: Importing/parsing CSV files
Re: Importing/parsing CSV files
- Subject: Re: Importing/parsing CSV files
- From: T&B <email@hidden>
- Date: Tue, 24 Oct 2006 12:25:44 +1000
Hi Has,
Thanks for your AppleScript calling Python solution, which I've
appended below since it was posted some time ago.
Unfortunately, I get an "End of File Error" when I try to use it with
a test csv file that's working OK with other techniques.
The line that causes the EOF error is:
set lst to (read POSIX file outpath as list)
when I comment this out and the subsequent return statement, it
executes in 0.23s on my large CSV test file, compared to 0.84s via my
best alternative method so far. Even with the added overhead of
successfully reading the the file into a list, this Python solution
looks promising.
How can I avoid or track down the error above?
Thanks,
Tom
----
From: has <email@hidden>
Date: 14 September 2006 5:03:42 AM
To: email@hidden
Subject: Re: Importing/parsing CSV files
T&B wrote:
In the absence of anyone else replying with a solution
The following script will read a csv file in the specified dialect
and write out a list file that can be read using Standard Additions'
read command. It's simple, fast and robust - Python's csv module is
well written and thoroughly tested - and will work out of the box on
10.3+ (for older OSes you'll have to install Python 2.3+ yourself).
-------------------------------------------------
property converterscript : "
import csv, struct, sys
dialect, inpath, outpath = sys.argv[1:]
# read csv file
csvfile = file(inpath)
table = list(csv.reader(csvfile, dialect))
csvfile.close()
# write list data to AS-readable file
f = file(outpath, 'w')
f.write('list' + struct.pack('L', len(table)))
for row in table:
f.write('list' + struct.pack('L', len(row)))
for cell in row:
s = cell.encode('utf16')
f.write('utxt' + struct.pack('L', len(s)) + s)
f.close()
"
on convertCSVFile(dialect, inpath, outpath)
-- converts a csv file to an AppleScript-readable list file
set s to "python -c"
repeat with arg in {converterscript, dialect, inpath, outpath}
set s to s & space & quoted form of arg
end repeat
do shell script s
end convertCSVFile
on readCSVFile(dialect, inpath)
set outpath to do shell script "mktemp -t converted_csv"
convertCSVFile(dialect, inpath, outpath)
set lst to (read POSIX file outpath as list)
do shell script "rm " & quoted form of outpath
return lst
end readCSVFile
-- TEST
readCSVFile("excel", "/Users/foo/tst.csv")
-------------------------------------------------
Note: when compiling the script, make sure the Python string contains
linefeeds, not carriage returns, as the Python interpreter ignores
the latter (e.g. SE 2 uses LFs but Smile uses CRs).
This version assumes the source file contains printable ASCII
characters only, but if it needs to work with other encodings (e.g.
UTF8) then that can be arranged. Adjusting the parser's minor options
can also be supported if necessary.
See <http://docs.python.org/lib/module-csv.html> for additional
documentation.
has
--
http://freespace.virgin.net/hamish.sanderson/
_______________________________________________
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/mailman//archives/applescript-users
This email sent to email@hidden