Hi Hans, If you're looking to store lists as lists, may I recommend the cPickle module? It's good for most times when you want to store an object as an object.
>>> import cPickle >>> a = [1,2,3] >>> b = [4,5,6] >>> c = [7,8,9] >>> d = [a,b,c] >>> d [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> print d[0][1] 2 >>> f = file("filetosaveto","wb") >>> cPickle.dump(d, f) >>> f.close() >>> del d >>> f = file("filetosaveto","rb") >>> newD = cPickle.load(f) >>> f.close() >>> newD [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> newD[0][1] 2 Regards, Liam Clarke On 12/28/05, Hans Dushanthakumar <[EMAIL PROTECTED]> wrote: > > Hi, > Is there any easy way of writing lists to a file and more > importantly, reading it back as a list of lists rather than as a list of > strings. > > Eg: > > >>> t = ["t1", "PASS", 31] > >>> f = open("pass.txt","a+") > >>> f.write(str(t) + "\n") > >>> f.write(str(t) + "\n") > >>> f.close() > > At this stage, the file contains two lines. > > Now, if I use the readlines() function to read it back, heres what I > get: > >>> f = open("pass.txt","r+") > >>> r = f.readlines() > >>> r > ["['t1', 'PASS', 31]\n", "['t1', 'PASS', 31]\n", "['t1', 'PASS', 31]\n"] > >>> r[0] > "['t1', 'PASS', 31]\n" > > So, r[0] is now a string. Is there ant direct way of extracting the list > from this string? > > Or alternatively, can I read the file as a list of lists rather than > list of strings (which is what readlines() appears to do). > > Thanks, > Hans > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor