Reading an CSV file as an array using AppleScript

I would like to read in a CSV file as a 2D array and then return it back to a CSV file. Let's say this is my CSV file. This is an excel file and | implies an adjacent cell:

family | type Doctor | Pediatrics Engineer | Chemical 

From what I understand, applescript doesn't have arrays, just lists and records. If it would be better to do this with an .xlsx file, please let me know.

+4
source share
3 answers

An array is just a list in applescript, so you need a 2d array or list of lists in applescript-talk. If you understand applescript text separators, then your task is simply to manipulate the conversion of strings to lists and vice versa. So I wrote you a couple of handlers to make the task easy for you; textToTwoDArray () and twoDArrayToText (). This first example shows how to convert your string to a list of lists using textToTwoDArray ().

NOTE. You should be careful with the line ends in a text file because they can be either carriage returns (character code 13) or a line (character identifier 10). You can see that I used the id 10 character in my code, but if you did not get the correct results, try "13".

 set fileText to "family | type Doctor | Pediatrics Engineer | Chemical" textToTwoDArray(fileText, character id 10, " | ") on textToTwoDArray(theText, mainDelimiter, secondaryDelimiter) set {tids, text item delimiters} to {text item delimiters, mainDelimiter} set firstArray to text items of theText set text item delimiters to secondaryDelimiter set twoDArray to {} repeat with anItem in firstArray set end of twoDArray to text items of anItem end repeat set text item delimiters to tids return twoDArray end textToTwoDArray 

Here's how to convert a list of lists to your string using twoDArrayToText ().

 set twoDArray to {{"family", "type"}, {"Doctor", "Pediatrics"}, {"Engineer", "Chemical"}} twoDArrayToText(twoDArray, character id 10, " | ") on twoDArrayToText(theArray, mainDelimiter, secondaryDelimiter) set {tids, text item delimiters} to {text item delimiters, secondaryDelimiter} set t to "" repeat with anItem in theArray set t to t & (anItem as text) & mainDelimiter end repeat set text item delimiters to tids return (text 1 thru -2 of t) end twoDArrayToText 

So now all you have to do is figure out how to read and write to a text file using applescript. Good luck;)

+4
source

Nigel CSV-to-list converter is the best I've seen ...

http://macscripter.net/viewtopic.php?pid=125444#p125444

As an example, use the following settings:

 set csvText to "family | type Doctor | Pediatrics Engineer | Chemical" csvToList(csvText, {separator:"|"}, {trimming:true}) 

V2

 set csvText to read "/Users/user1385816/Desktop/yourfile.csv" csvToList(csvText, {}, {trimming:true}) 
+4
source

If your question is about modeling your CSV data in AppleScript, the solution should use a list of these entries:

 set csvData to {{family:"Doctor", type:"Engineer"}, {family:"Engineer", type:"Chemical"}} 

you can read it again inside the repeat with aRow in csvData .

Technical Note AppleScript lists are implemented as vectors, that is, they are ordered and can be accessed using a numerical index (starting at index 1) - they behave like an array in most other languages.

0
source

Source: https://habr.com/ru/post/1412193/


All Articles