How do you undo x = `a` if A is a list?

Trent Mick trentm at ActiveState.com
Fri Feb 11 08:14:35 EST 2000


> Now how do I read that line in from a file and convert it from a
> string to a
> list?


Here is one way. Save your variable in the proper format to a Python module.
Don't know if I would recommend it. As Skip et al. suggest, eval() and its
kin are probably better.

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import sys
>>> a = [1,2,3]
>>> output = open('save.py','w')
>>> output.write("a = " + str(a))
>>> output.close()
>>> ^Z

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from save import *
>>> a
[1, 2, 3]


Trent





More information about the Python-list mailing list