View Single Post
Old 02-05-2007, 02:13 PM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Quote:
Originally Posted by teknomage1
maybe redhead will respond since he actually uses python
Sorry tekno, I dont use python at a regular basis, I know of it and how it should be used, but I'm not currently involved in projects developed in the language. Thats why I havn't listed it as one of my known programming languages.

QUantumAnenome, In a case like the one you're looking for, you'd use the Comma Separated Values module. Simply have your file comma seperated, or tab or whatever you like, and use the module to convert it back to your orriginal list.

Say you're using Excel as the delimiter you can store and retrieve your data in a matter like this:
Code:
import csv

a=[1,2,3,[4,5],6,[7,8,[9]],0]

writer = csv.writer(open("my_file", "wb"), csv.excel)
writer.writerows(a)
writer.close()

b=array()
reader = csv.reader(open("my_file", "rb"), csv.excel)
try:
    for row in reader:
        b.append(row)
except csv.Error, e:
    sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))
reader.close()

print a
print b
Or perhaps you just want the easy and quick answer to this one... By using the pickle serialization
Code:
import pickle
a = [1,2,3,[4,5,6],7,[8,[9]],0]
output = open('my_file', 'wb')
pickle.dump(a, output)
output.close()

input = open('my_file', 'rb')
b = pickle.load(input)
input.close()

print a
print b

a == b
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 02-05-2007 at 02:43 PM.
redhead is offline   Reply With Quote