reading a list from a file
kveretennicov at gmail.com
Tue Jun 21 02:41:19 CEST 2005
On 6/20/05, David Bear <david.bear at 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], t) for t in tokens])
More information about the Python-list