reading a list from a file

D

David Bear

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 thinking I
would have to

read until char = '['
read until char = " ' "

Well, reading character by char until I have the list and then parse it all
myself.

I was hoping there was a pythonic way of doing this..
 
T

Tim Williams

----- Original Message -----
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 thinking I
would have to


I was hoping there was a pythonic way of doing this..

a = '[1,2,3,4,5]'
b = eval(a)
b [1, 2, 3, 4, 5]
b[2]
3

HTH :)
 
J

Jordan Rastrick

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 cause that code to be
executed.

So, to put it bluntly, eval is dangerous.

Sure is convenient, though :)
 
J

Jordan Rastrick

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 pretty easy. The problem is the list
elements are still in string form.

If they're only integers like in this example, you're fine. If they're
arbitrary objects, on the other hand, well then you may need eval after
all.

Question is, how did they get printed to a file in this form in the
first place?
 
R

Rune Strand

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()
el = el.replace("'", "")
lists.append(el.split(','))

# further processing of lists

but the type problem is still to be resolved ;-)
 
J

John Machin

Rune said:
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()
el = el.replace("'", "")
lists.append(el.split(','))

# further processing of lists

but the type problem is still to be resolved ;-)

Try this:

["O'Reilly, Fawlty, and Manuel", '""', ',', '"Hello", said O\'Reilly']

:)
 
K

Konstantin Veretennicov

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 objects?
Is there an easy way to read this file into a list again?
I'm thinking I would have to

read until char = '['
read until char = " ' "

Well, reading character by char until I have the list and then
parse it all myself.

At least you can leave tokenizing to python lib:
import tokenize, token, pprint
token_names = dict([
.... (value, name) for (name, value)
.... in token.__dict__.iteritems()
.... if isinstance(value, int)])
tokens = tokenize.generate_tokens( .... iter(['[1, 2, "ab\'c,d"]']).next)
pprint.pprint([(token_names[t[0]], t[1]) for t in tokens])
[('OP', '['),
('NUMBER', '1'),
('OP', ','),
('NUMBER', '2'),
('OP', ','),
('STRING', '"ab\'c,d"'),
('OP', ']')]

- kv
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top