getting a csv file into a list of lists

I was trying to get a csv file to fill a list of lists in an AS Studio project.

I have this:


	--get the csv file
	set slFile to ((path to library folder from user domain) & "test.txt") as Unicode text
	set tempData to read file slFile 
	--save the ASTIDs
	set ASTID to AppleScript's text item delimiters
	--make them a comma
	set AppleScript's text item delimiters to {","}
	--clear the list
	set MyList to {} as list
	--repeat thru the text by paragraph
	repeat with aLine in (paragraphs of tempData)
		set itemList to {} as list
		repeat with aItem in (text items in aLine)
			set end of itemList to aItem as list
		end repeat
		set MyList to (MyList & itemList) as list
	end repeat

This makes one list with the items or {item1A, item2A, item1B, item2B, etc}

What I am trying for is {{item1A, Item2A}, {item1B, item2B}, etc}

What am I doing wrong?

Thanks
Dee

Hi deedeew,

I guess you have to change the second to last line of your code:


 set MyList to MyList & {itemList}

Here is another piece of example code I made up for you:


set textlist to {"Here I go again!", "Let's have a party!", "Don't worry about the future!"}

set matrix to {}
repeat with textstring in textlist
	set wordlist to {}
	repeat with textitem in (text items in textstring)
		set wordlist to wordlist & textitem
	end repeat
	set matrix to matrix & {wordlist}
end repeat

Hi Martin,

a better (and faster) way to append an item to a list is

set end of MyList to itemList

Hi Stefan,

you are absolutely right, but here on my Mac this won’t result in a nested list as required by deedew :wink:

Best regards to Switzerland!

Thanks to you both for the assistance!

It appears that both methods work on my Mac.

Don’t know if this makes any diff, but…
MacBook Pro
Mac OS 10.5.4
X Code 3.0

Thanks again
Dee

In the interest of beating a dead horse…

I have code that works but was thinking that this could get awfully slow if the CSV file was quite large. Here’s the code as it sits…


on makeList(txtData)
	--save the current delimiters
	set ASTID to AppleScript's text item delimiters
	--reset them to commas
	set AppleScript's text item delimiters to ","
	--reset the tempList
	set tempList to {} as list
	--start paragraph loop
	repeat with aLine in (paragraphs of txtData)
		--reset the item list
		set itemList to {} as list
		--start the item loop
		repeat with aItem in (text items of aLine)
			--add an item by commas
			set end of itemList to aItem as list
		end repeat
		--add item list to the end of main list
		set end of tempList to itemList
	end repeat
	--return the list
	return tempList
end makeList

Posted here with comments as that I had not seen anything like this posted here before. This maybe because of the search restrictions that ignored looking for “CSV” or my own impatience in combing through the results to find a gem.

This routine is given the data read from the CSV file as text. It returns a list of lists. While this is exactly what my intentions were, I was wondering if anyone else had come up with something faster??

I have other methods for dealing with this so that it does not negatively impact the user experience, but it might be nice.

Dee

Model: MacBook Pro 2.5GHz
AppleScript: X Code 3
Browser: Safari 525.20
Operating System: Mac OS X (10.5)

This might be faster


on makeList(txtData)
	set TID to text item delimiters
	set text item delimiters to ","
	set tempList to {}
	repeat with aLine in (paragraphs of txtData)
		set end of tempList to text items of aLine
	end repeat
	set text item delimiters to TID
	return tempList
end makeList

But consider, that CSV could be more than a few items separated by commas.
What’s happening if the items contain quotes, commas or CR/LF characters ?? :wink:

http://en.wikipedia.org/wiki/Comma-separated_values

Thanks,
I have considered this. The CSV that I am using is controlled. There will be no extraneous data because the system that generates the file cannot handle it. I lucked out on that one.

Best
Dee

This was indeed faster, it did not, however, make a list of lists (i.e.: {item 1A, item 2A}, {item 1B, item 2B}, etc). It made a list of multiple items.

Dee

Sorry, my mistake. It did work!

Thanks
Dee