By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,932 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

reading a list from a file

P: n/a
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..

--
David Bear
-- let me buy your intellectual property, I want to own your thoughts --
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

----- Original Message -----
From: "David Bear" <da********@asu.edu>

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 :)
Jul 19 '05 #2

P: n/a
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 :)

Jul 19 '05 #3

P: n/a
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?

Jul 19 '05 #4

P: n/a
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 ;-)

Jul 19 '05 #5

P: n/a
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 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']

:-)
Jul 19 '05 #6

P: n/a
On 6/20/05, David Bear <da********@asu.edu> 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 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
Jul 19 '05 #7

P: n/a
:-/ You're right!

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.