Convert content of txt file back to a list of lists

Hello Scripter,

i have a textfile with a few thousand records. They are all generated by another script. The output was a list that was stored in the txt file for performance reasons (it took several minutes to generate the list by spliting and matching).

If I read in the content of the textfile I get a long string with all quotes escaped. Like this:

string after reading contents of the .txt file

 "{{\"2014\", \"1\", \"149x\", \"149\"}, {\"2014\", \"2\", \"150x\", \"150\"}, {\"\", \"\", \"\", \"\", \"\", \"\", \"\"}, {\"2015\", \"3\", \"151b\", \"177\"}, {\"2015\", \"4\", \"152a\", \"178\"}, {\"2015\", \"5\", \"153\", \"179\"}, {\"2015\", \"6\", \"154x\", \"180\"}, {\"2015\", \"6A\", \"154y\", \"180a\"}"

But what I need is again a list of lists. How can i process this to ge back the original list with the containing listitems as strings?

a list of lists is what I need again. Like this:

{{"2014", "1", "149x", "149"}, {"2014", "2", "150x", "150"}, {"", "", "", "", "", "", ""}, {"2015", "3", "151b"}, {"2015", "4", "152a", "178"}, {"2015", "5", "153", "179"}, {"2015", "6", "154x", "180"}, {"2015", "6A", "154y", "180a"}}

You may use this old fashioned script :

set theSource to "{{\"2014\", \"1\", \"149x\", \"149\"}, {\"2014\", \"2\", \"150x\", \"150\"}, {\"\", \"\", \"\", \"\", \"\", \"\", \"\"}, {\"2015\", \"3\", \"151b\", \"177\"}, {\"2015\", \"4\", \"152a\", \"178\"}, {\"2015\", \"5\", \"153\", \"179\"}, {\"2015\", \"6\", \"154x\", \"180\"}, {\"2015\", \"6A\", \"154y\", \"180a\"}"
set temp to my remplace(theSource, "{{", "")
set temp to my remplace(temp, "}, {", linefeed)
set temp to my remplace(temp, "}", "")
set asList to paragraphs of temp

repeat with i from 1 to count asList
	set itemI to asList's item i
	set temp to my remplace(itemI, "\", \"", return)
	set temp to my remplace(temp, "\"", "")
	set item i of asList to paragraphs of temp
end repeat

asList

#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end remplace

#===== 

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) samedi 27 aout 2016 16:16:18

Hi,

as you’re writing about performance reasons why don’t use simply write the list to disk as it is

set theList to {{"2014", "1", "149x", "149"}, {"2014", "2", "150x", "150"}, {"", "", "", "", "", "", ""}, {"2015", "3", "151b"}, {"2015", "4", "152a", "178"}, {"2015", "5", "153", "179"}, {"2015", "6", "154x", "180"}, {"2015", "6A", "154y", "180a"}}

set theFile to (path to desktop as text) & "testList.txt"
try
	set fileDescriptor to open for access file theFile with write permission
	write theList to fileDescriptor
	close access fileDescriptor
on error
	try
		close access file theFile
	end try
end try

and read it back

set theFile to (path to desktop as text) & "testList.txt"
set theList to read file theFile as list

Thank you both for your help! I am always amazed how quick people answer here. Learned something good again.

I went with Stefans approach and wrote all records to a file again. Now it reads everything fine as a list.
I forgot the as list part.

Thanks again and have nice Weekend.

Just for the record, you list was text based and you could turn text into code using run script command:

run script "{{\"2014\", \"1\", \"149x\", \"149\"}, {\"2014\", \"2\", \"150x\", \"150\"}, {\"\", \"\", \"\", \"\", \"\", \"\", \"\"}, {\"2015\", \"3\", \"151b\", \"177\"}, {\"2015\", \"4\", \"152a\", \"178\"}, {\"2015\", \"5\", \"153\", \"179\"}, {\"2015\", \"6\", \"154x\", \"180\"}, {\"2015\", \"6A\", \"154y\", \"180a\"}}"

Oh that is interesting. I did not now that. Takes also care of the escaped quotes :).
Learn something new every day.