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

Emile van Sebille emile at fenx.com
Sat Feb 12 00:23:40 EST 2000


Warren,

What you're asking for is the eval(repr(x)) pair.

>>> a = [ "first", "second", "third" ]
>>> f = open(r"c:\temprepr","w")
>>> f.write(repr(a))

then later...

>>> f = open(r"c:\temprepr","r")
>>> c = eval(f.readline)
>>> c
['first', 'second', 'third']

HTH

Emile van Sebille
emile at fenx.com
-------------------


----- Original Message -----
From: Warren Postma <embed at geocities.com>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Friday, February 11, 2000 12:39 PM
Subject: How do you undo x = `a` if A is a list?


> 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
>
>
>
> --
> http://www.python.org/mailman/listinfo/python-list
>






More information about the Python-list mailing list