Characters to Lists

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Characters to Lists

 
0
  #1
Apr 17th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Characters to Lists

 
0
  #2
Apr 17th, 2007
is this what you are looking for
  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. >>>
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Re: Characters to Lists

 
0
  #3
Apr 17th, 2007
No, sorry... I should have been more clear. This is what I want:

  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:

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

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

  1. for i in x:
  2. print i
  3.  
  4. 1
  5. 2
  6. 3
  7. 4
Make more sense now?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Characters to Lists

 
1
  #4
Apr 17th, 2007
if in the file, the content is indeed this:
  1. [1,2,3,4]
then when you read the file line by line, you can use eval() to turn it into a list
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,528
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 22
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: Characters to Lists

 
1
  #5
Apr 17th, 2007
To save and load a whole object like a list, you have to use the module pickle:
  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]
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 77
Reputation: aot is an unknown quantity at this point 
Solved Threads: 1
aot's Avatar
aot aot is offline Offline
Junior Poster in Training

Re: Characters to Lists

 
0
  #6
Apr 17th, 2007
Ahh, thanks! You guys are the best!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC