1

As said in the title of the thread.
Also I tried using eval() for these lists I'll be inputting, but these lists contain string elements and other (sub-?) lists, e.g:

[Pacific, [-45,30,25], [120,59, 15]]

When providing such input eval() responds perfectly fine for numbers-only lists but when applied to strings sends back a NameError for these string elements saying they are not defined.

timss
  • 9,982
  • 4
  • 34
  • 56
Dazcii
  • 41
  • 4

2 Answers2

0

you need to be inputting your strings within quotations, ie,

["Pacific", [-45,30,25], [120,59, 15]]

when you input it just as Pacific, python's eval() function will look for a variable named Pacific, hence the NameError you have been getting

Cameron Sparr
  • 3,925
  • 2
  • 22
  • 31
  • no prob! if this answered your question, please give it a green checkmark or an upvote ^^ – Cameron Sparr Apr 23 '13 at 20:25
  • 1
    Perhaps you can warn him of the [dangers of `eval`](http://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval) and point him at Use `ast.literal_eval`. See http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html – Robᵩ Apr 23 '13 at 20:29
  • `eval()` is only dangerous for untrusted input, if you read his post you'll see that he states that he will be inputting the lists himself, so as long as OP trusts himself....... – Cameron Sparr Apr 24 '13 at 05:47
0

Your original input string is perfectly valid YAML string, which is a safe, powerful and -at least in my view- easy way to serialize and de-serialize data structures. In order to read (or save) YAML strings you might want to get PyYAML:

sudo pip install pyyaml

Then, you can perfectly run the following code in order to read your data into Python:

from yaml import load
pacific = load('[Pacific, [-45,30,25], [120,59, 15]]')
GermanK
  • 1,676
  • 2
  • 14
  • 21