ast.literal_eval()
Python literal_eval is a function defined in “ast” class of built-in class library. The “ast” expands to Abstract Syntax Tree. The ast module helps Python applications to process trees of the Python abstract syntax grammar.
The literal_eval safely evaluate an expression node or a string containing a Python literal or container display. The string or node (a file) provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.
Syntax: ast.literal_eval ( node_or_string )
Understand with example
In simple word, literal_eval function, of ast class, helps to find the type of value stored in a file (some times are known as a node) or strings. Most of you would have learned to work with datatypes or data structure in Python. We can easily create and manipulates these data structures during program execution (or until it is managed by the compiler). We can also write these data structures in files.
But the Game start from here
When you try to read these file using Python File handling methods, it is easy to read and display the content of the file in Python. We can even understand as which datatype is stored in the file such as a dictionary, list, tuple, boolean or strings or any other.
Related code
Create a dictionary in Python and write this to a text file. Python code of the same is available below.
dict1 = {'aipython': 2018, 'location': 'India', 'presence':'Internet'}
file_writer = open( r'C:\Document\myDictionary.txt', 'w+' )
file_writer.write(str(dict1))
file_writer.close()
The text file (with contents) will look similar to the image given below.
My question to you – Can you read this file in Python and let the Python tell the datastructure (or datatype) of the contents?
Python code to read the contents of this text file and the type of the object holding this value. You will notice that the file type (or data type of file object) is something strange.
file_reader = open( r'C:\Document\myDictionary.txt', 'r' )
print (file_reader.read())
print (type(file_reader))
For us (human) it is very easy to tell that it is a dictionary data structure, but for Python, it is not easy, until it uses literal_eval function of ast class.
Now you will see how to make use of literal_eval to recognize the data type of stored contents.
import ast
file_reader = open( r'C:\Document\myDictionary.txt', 'r' )
temp_data = ast.literal_eval( file_reader.read( ) )
print (temp_data)
print ("Data type of object is: {}".format(type(temp_data)))
Limitation: literal_eval can recognize only one data type. If the file contains elements having more than one data structure then it will throw an error. Supported Python literal structures are strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.
Conclusion – Python ast.literal_eval
After reading you have understood about literal_eval function and why it is used in Python. You have also understood to use this function to get the data type of the contents. This function helps to find all the data types defined in the Python programming language. Read more about ast class at the official website.
Keep Learning and Keep Growing !!
great explanation with simple words and good examples