954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Characters to Lists

So I'm at the final stages of my program -- it's a working experiment that creates a data file for each participant. My hope was that when I was done, it would be relatively easy to create an analysis program that searches through all the data files and create a nice tab delimited text file that's ready to pop into Excel or SPSS.

However, what I have learned is that, while it's easy to write a list into a string in a file, it's much more difficult to turn it around -- I want to be able to read that same string and turn it right back into a list again. Then I can go through each file, taking the numbers out of the lists written for each participant and putting them all together in one file.

I can't do that if it creates a list character by character, though. Is there some way to maybe mark in the file "this is a list" so that it can be put back together into one later? Or is there another way to store lists of data that I'm not thinking of? Are there any other solutions to my problem?

Right now it looks like I'm going to have to change the way each data file is written to begin with (so that each member of a list is written on a line by itself), then do a lot of copying and pasting -- something of a pain. I would massively appreciate any input on this problem!

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

is this what you are looking for

>>> s = "this is a string"
>>> alist = list(s)
>>> alist
['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g']
>>>
ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

No, sorry... I should have been more clear. This is what I want:

list = [1,2,3,4]
write(list)



In the file I see:
[1,2,3,4]

I want to be able to get that back as a list. But if I do:

fileHandle = open(file, 'r')
x = fileHandle.readline()
list(x)
print x



I get:

['[','1', ',', '2', ',', '3', ',', '4', ']']



I want: [1,2,3,4], a list that I can then use, as in:

for i in x:
    print i

1
2
3
4



Make more sense now?

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

if in the file, the content is indeed this:

[1,2,3,4]

then when you read the file line by line, you can use eval() to turn it into a list

for line in open("file"):
    alist =  eval(line)
    #do something with alist.
ghostdog74
Junior Poster
156 posts since Apr 2006
Reputation Points: 75
Solved Threads: 44
 

To save and load a whole object like a list, you have to use the module pickle:

import pickle
 
myList = [1,2,3,4]
 
# save/dump the list as an object
file = open("myList.dat", "w")
pickle.dump(myList, file)
file.close()
 
# read/load the list as an object
file = open("myList.dat", "r")
myList2 = pickle.load(file)
file.close()
 
# testing
print myList   # [1, 2, 3, 4]
print myList2  # [1, 2, 3, 4]
Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
 

Ahh, thanks! You guys are the best!!

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You