| | |
Characters to Lists
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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!
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!
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
is this what you are looking for
Python Syntax (Toggle Plain Text)
>>> s = "this is a string" >>> alist = list(s) >>> alist ['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g'] >>>
No, sorry... I should have been more clear. This is what I want:
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:
I get:
I want: [1,2,3,4], a list that I can then use, as in:
Make more sense now?
Python Syntax (Toggle Plain Text)
list = [1,2,3,4] write(list)
[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)
fileHandle = open(file, 'r') x = fileHandle.readline() list(x) print x
Python Syntax (Toggle Plain Text)
['[','1', ',', '2', ',', '3', ',', '4', ']']
Python Syntax (Toggle Plain Text)
for i in x: print i 1 2 3 4
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
if in the file, the content is indeed this:
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,2,3,4]
Python Syntax (Toggle Plain Text)
for line in open("file"): alist = eval(line) #do something with alist.
Last edited by ghostdog74; Apr 17th, 2007 at 1:06 pm.
To save and load a whole object like a list, you have to use the module pickle:
python Syntax (Toggle Plain Text)
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]
![]() |
Similar Threads
- strcpy and linked lists (C++)
- LCD backlight on, no characters (Monitors, Displays and Video Cards)
- LCD backlit, but no characters. (Monitors, Displays and Video Cards)
- Receive Windows Messenger Messages and Alerts on your Mobile Device (Windows tips 'n' tweaks)
Other Threads in the Python Forum
- Previous Thread: here it is
- Next Thread: Scrollbar
| Thread Tools | Search this Thread |
abrupt ansi anti approximation array assignment avogadro backend beginner binary bluetooth builtin calculator character chmod code converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary drive dynamic examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse mysqlquery number numbers output parsing path plugin pointer port prime programming progressbar projects py2exe pygame pyqt pysimplewizard python random recursion scrolledtext sqlite statistics stdout string strings sum table terminal text textarea thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows write wxpython xlib






