:-/ You're right!
--
http://mail.python.org/mailman/listinfo/python-list
On 6/20/05, David Bear <[EMAIL PROTECTED]> wrote:
> I have a file that contains lists -- python lists. sadly, these
> are not pickled. These are lists that were made using
> a simple print list statement.
Sad, indeed. But what kind of objects they held? Only ints? Ints and
strings? Arbitrary objec
Rune Strand wrote:
> But iif it are many lists in the file and they're organised like this:
>
> ['a','b','c','d','e']
> ['a','b','c','d','e']
> ['A','B','C','D','E'] ['X','F','R','E','Q']
>
> I think this'll do it
>
> data = open('the_file', 'r').read().split(']')
>
> lists = []
> for el in dat
But iif it are many lists in the file and they're organised like this:
['a','b','c','d','e']
['a','b','c','d','e']
['A','B','C','D','E'] ['X','F','R','E','Q']
I think this'll do it
data = open('the_file', 'r').read().split(']')
lists = []
for el in data:
el = el.replace('[', '').strip()
If you decide to steer clear of eval, the following comes close to what
you want, and is somewhat Pythonic (I feel):
def back_to_list(str):
return str.strip("[]").split(", ")
>>> s = "[1, 2, 3, 4, 5, 6]"
>>> back_to_list(s)
['1', '2', '3', '4', '5', '6']
So parsing the list structure is prett
Be careful, though - make sure you can absolutely trust your source of
data before calling eval on it.
If an unauthorised person could forseeably modify your file, then they
could insert a string containing arbitrary Python code into it in place
of your list, and then running your program would ca
- Original Message -
From: "David Bear" <[EMAIL PROTECTED]>
> I have a file that contains lists -- python lists. sadly, these are not
> pickled. These are lists that were made using a simple print list
> statement.
>
> Is there an easy way to read this file into a list again? I'm thin