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

Warren Postma embed at geocities.com
Fri Feb 11 15:39:29 EST 2000


Let's suppose I have a list A:

a = [ "first", "second", "third" ]

And I want to save to a text file on disk the following Exactly into
myfile.txt.
I looked at Pickle, but "human readable" is not one of Pickle's features.

[ "first", "second", "third" ]

So that part is easy, I can convert like this:

a_as_text = `a`

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

If I was using my old favourite lange (Realizer, a Basic interpreter)  I
would do this:

        Execute( "a = "+a_as_text)  # dynamic invocation of the Parser (slow
but powerful!)

The benefit of this is that it would allow any one line of Ascii text to be
converted into the corresponding Python object, totally dynamically.

Sorry if this is a FAQ but I couldn't find it in my scan of the
documentation and FAQ.

Warren






More information about the Python-list mailing list