0

with eval I am able to get the below:

str1 = "23145"
str2 = "str1"
print(str2)

Output:

23145

working well and good.

with ast.literal_eval that is not working as expected:

import ast
print(ast.literal_eval(str2))

Getting:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Volumes/app/python/anaconda3/lib/python3.7/ast.py", line 91, in literal_eval
    return _convert(node_or_string)
  File "/Volumes/app/python/anaconda3/lib/python3.7/ast.py", line 90, in _convert
    return _convert_signed_num(node)
  File "/Volumes/app/python/anaconda3/lib/python3.7/ast.py", line 63, in _convert_signed_num
    return _convert_num(node)
  File "/Volumes/app/python/anaconda3/lib/python3.7/ast.py", line 55, in _convert_num
    raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Name object at 0x10ce34190>
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Does this help? https://stackoverflow.com/q/32695699/259889 – Sid May 20 '20 at 16:13
  • First of all you don't actually use `eval` in the first example. Second, [`literal_eval`](https://docs.python.org/3/library/ast.html#ast.literal_eval) is only for, as its name suggests, literals, not names: *The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.* – Tomerikoo May 20 '20 at 16:14
  • Does this answer your question? [ValueError: malformed node or string with ast.literal\_eval() when adding a Keras layer](https://stackoverflow.com/questions/51711688/valueerror-malformed-node-or-string-with-ast-literal-eval-when-adding-a-keras) – Tomerikoo May 20 '20 at 16:18

1 Answers1

0

got the solution using globals().

str1 = “shiva”
str2 = “str1"
print(globals()[str2])

O/P: shiva