Parsing a CSV file and save each line into a new file

Hello,

I am new and fully beginner to applescript. Can applescript parse a .CSV file, split each line removing the first column and save it using the first column which has been removed as a filename?

Ex:

CSV file is “library.csv” containing:

“travel”,“list of travel”
“notes”,“list of notes”
“quotes”,“list of quotes”
etc…

I would like to have as many individual files as the number of lines of “library.csv” splitted like this (and the double quotes removed):

File 1 = “travel.txt” containing

list of travel

File 2 = “notes.txt” containing

list of notes

File 3 = “quotes.txt” containing

list of quotes

etc…

Any help would be really appreciated.

Thank you in advance and best regards
Henri

Hi,

try this …

set destFolder to choose folder with prompt "Please select the Destinationfolder"

set myArray to paragraphs of (read (choose file with prompt "Please choose the CSV-File"))

set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","

repeat with i from 1 to count of myArray
	set fName to text 2 thru -2 of (text item 1 of (item i of myArray))
	set destPath to (destFolder as text) & fName & ".txt"
	
	set myContent to text 2 thru -2 of (text item 2 of (item i of myArray))
	
	my write2file(destPath, myContent)
end repeat

set TID to AppleScript's text item delimiters


on write2file(thisFile, thisText)
	try
		open for access file the thisFile with write permission
		write (thisText) to file the thisFile starting at eof
		close access file the thisFile
	on error
		try
			close access file the thisFile
		end try
	end try
end write2file

Hello Hans,

Wouahhh thanks a lot that is very fast and really appreciated.

I just tested it and it works almost at 100%.

I jsut have two small problems:

  1. my delimiter is “,” and not only ,
    Then, if my line contains for example the following:
    “quotes”,“list of quotes, citations, blalbla”
    then the output is only: list of quotes

  2. I may have french accentued characters into the first column and then the file name is strange like:
    À l’oeil.txt
    instead of
    À l’oeil.txt

Apart from that is works greats.

Again thanks a lot and regards.
Henri

Hi,

point 1 should be solved
point 2 depends on specific text file encoding (difficult without having original data to test with)

try this:


--tested with operating system 10.7.4
set destFolder to choose folder with prompt "Please select the Destinationfolder"

set myArray to paragraphs of (read (choose file with prompt "Please choose the CSV-File") as «class utf8»)

set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\",\""

repeat with i from 1 to count of myArray
	set fName to text 2 thru -1 of (text item 1 of (item i of myArray))
	set destPath to (destFolder as text) & fName & ".txt"
	
	set myContent to text 1 thru -2 of (text item 2 of (item i of myArray))
	
	my write2file(destPath, myContent)
end repeat

set TID to AppleScript's text item delimiters


on write2file(thisFile, thisText)
	try
		open for access file the thisFile with write permission
		write (thisText) to file the thisFile starting at eof
		close access file the thisFile
	on error
		try
			close access file the thisFile
		end try
	end try
end write2file

Hi Hans,

This is perfect.

Thanks a lot. This will spare me hours of manual works.

Regards and nice week-end
Henri

Hi,

fine.

The line after “end repeat” should have been “set AppleScript’s text item delimiters to TID” to set them back …

Hi Hans,

Done.

Thanks again :slight_smile:

Regards.
Henri

StefanK showed me a lovely way to set AppleScript’s text item delimiters:-


set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
-- Process Text...
set AppleScript's text item delimiters to atid

Thought I’d post it in case it’s of use to anyone, I use this method pretty much all the time now.