Re: Importing/parsing CSV files
Re: Importing/parsing CSV files
- Subject: Re: Importing/parsing CSV files
- From: has <email@hidden>
- Date: Wed, 13 Sep 2006 20:03:42 +0100
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:
This email sent to email@hidden