how to read list from file

Jussi Piitulainen jpiitula at ling.helsinki.fi
Mon Oct 7 02:04:16 EDT 2013


Harvey Greenberg writes:

> On Saturday, October 5, 2013 7:24:39 PM UTC-6, Tim Chase wrote:
> >   >>> s = "[{'a':1, 'b':2}, [1,2,3], 10]"
> >   >>> import ast
> >   >>> print repr(ast.literal_eval(s))
> >   [{'a': 1, 'b': 2}, [1, 2, 3], 10]
>
> that didn't work.  printing it looks like the list because it's the
> input, but try printing len(repr(ast.literal_eval(s))).  It should
> give 3, but it gives 72 (number of chars).

Not sure what the "print repr( )" is meant to achieve here, but I
think you should be able to see through it:

  >>> ast.literal_eval(s)
  [{'a': 1, 'b': 2}, [1, 2, 3], 10]
  >>> len(ast.literal_eval(s))
  3
  >>> 



More information about the Python-list mailing list