943,917 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1250
  • Python RSS
Apr 17th, 2007
0

Characters to Lists

Expand Post »
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!
Similar Threads
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Apr 17th, 2007
0

Re: Characters to Lists

is this what you are looking for
Python Syntax (Toggle Plain Text)
  1. >>> s = "this is a string"
  2. >>> alist = list(s)
  3. >>> alist
  4. ['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g']
  5. >>>
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Apr 17th, 2007
0

Re: Characters to Lists

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

Python Syntax (Toggle Plain Text)
  1. list = [1,2,3,4]
  2. 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:

Python Syntax (Toggle Plain Text)
  1. fileHandle = open(file, 'r')
  2. x = fileHandle.readline()
  3. list(x)
  4. print x
I get:

Python Syntax (Toggle Plain Text)
  1. ['[','1', ',', '2', ',', '3', ',', '4', ']']
I want: [1,2,3,4], a list that I can then use, as in:

Python Syntax (Toggle Plain Text)
  1. for i in x:
  2. print i
  3.  
  4. 1
  5. 2
  6. 3
  7. 4
Make more sense now?
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007
Apr 17th, 2007
1

Re: Characters to Lists

if in the file, the content is indeed this:
Python Syntax (Toggle Plain Text)
  1. [1,2,3,4]
then when you read the file line by line, you can use eval() to turn it into a list
Python Syntax (Toggle Plain Text)
  1. for line in open("file"):
  2. alist = eval(line)
  3. #do something with alist.
Last edited by ghostdog74; Apr 17th, 2007 at 1:06 pm.
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Apr 17th, 2007
1

Re: Characters to Lists

To save and load a whole object like a list, you have to use the module pickle:
python Syntax (Toggle Plain Text)
  1. import pickle
  2.  
  3. myList = [1,2,3,4]
  4.  
  5. # save/dump the list as an object
  6. file = open("myList.dat", "w")
  7. pickle.dump(myList, file)
  8. file.close()
  9.  
  10. # read/load the list as an object
  11. file = open("myList.dat", "r")
  12. myList2 = pickle.load(file)
  13. file.close()
  14.  
  15. # testing
  16. print myList # [1, 2, 3, 4]
  17. print myList2 # [1, 2, 3, 4]
Reputation Points: 407
Solved Threads: 36
Posting Virtuoso
Lardmeister is offline Offline
1,701 posts
since Mar 2007
Apr 17th, 2007
0

Re: Characters to Lists

Ahh, thanks! You guys are the best!!
aot
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
aot is offline Offline
83 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: here it is
Next Thread in Python Forum Timeline: Scrollbar





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC