Reputation: 23
I have been digging around for this for a while and can't find a solution. I have a CSV with for example the following data:
1,2,3,a,b,c,4,5,6,d,e,f,7,8,9,0
0,9,8,g,h,i,7,6,4,k,l,m,5,4,3,2
etc...
Each line contains mostly different data except for certain fields that are either true or false.
What i need is to pull each line as a list to then be able to parse the data.
I want to be able to remove, for example item number 3 from all the lists and then save the data to be used in a spreadsheet.
Please keep in mind I am relatively new to applescript so still trying to get my fingers on it, have bought a book that seems highly recommended on numerous sites but it's not delivered yet! :-)
Also if you know a different/better way of doing this then I would be happy to try.
At the moment I have the following:
Blockquote -- Setting Variables
set LF to ASCII character 10
-- Choosing your files
set prompt1 to "Please locate your Devices CSV file..."
set csvDevices to "testfile.csv"
--set csvDevices to (choose file with prompt prompt1)
-- Reading file to memory
set csvLines to read csvDevices
set AppleScript's text item delimiters to {LF}
set csvEntries to paragraphs of csvLines
set AppleScript's text item delimiters to {","}
-- Filtering rubbish
set csvNoTitle to rest of csvEntries -- Remove Title Line
set lineCount to count csvNoTitle
after that I am able to print out the contents but it will give me
{"1,2,3,a,b,c,4,5,6,d,e,f,7,8,9,0","0,9,8,g,h,i,7,6,4,k,l,m,5,4,3,2","etc..."}
rather than what I would like
{"1","2","3","a","b","c","4","5","6","d","e","f","7","8","9","0"}
{"0","9","8","g","h","i","7","6","4","k","l","m","5","4","3","2"}
{"etc..."}
by the way I remove the first line in the script as it's a description line with no actual useful data, or not to me anyway.
Thanks for any help
Upvotes: 2
Views: 2357
Reputation: 15847
You should break the text CSV file into records (single lines) and then break each record into values (separated by commas). You may proceed this way from the point you have the CSV text file into a string (csvLines).
-- getting records (you don't need to set applescript's text item delimiters here)
set recs to paragraphs of csvLines
-- getting values for each record
set vals to {}
set applescript's text item delimiters to ","
repeat with i from 1 to length of recs
set end of vals to text items of (item i of recs)
end repeat
-- this is true
(item 1 of vals) = {"1","2","3","a","b","c","4","5","6","d","e","f","7","8","9","0"}
This will parse the entire file without filtering. But is just to give the idea of correct CSV parsing. You already know how to filter the data.
Upvotes: 2