Python ast.literal_eval()
Is a very useful method which can evaluate a string contents for python datatypes . If the string happens to have valid datatype inside the string it will initialised . One use case in which I personally used it was to convert strings values stored csv files . The rows contained "list" as string and wanted to run operations on it. These must be converted into list format first ; ast.literal_eval() achieves that !
#python code
import ast
#string containing a list
stringed_list= "[1,2,3]"
#converting stringed list into python list
converted_list = ast.ast.literal_eval(string_list)
print(type(stringed_list)) # 'str'
print(type(converted_list)) # 'list'
Even though this is a really convenient way to convert stringed list back into python list , it is slow. This works great for moderately small csv data files in which we can store scaled parameter lists , etc as string .
When size of the file increases so does the processing time and it is very noticeable. So if your csv files contains few million rows of values or more , it is better to store the data by python pickling.
Python pickling is considerably faster at least 3 to 5 times than running "literal_eval".
Comments
Post a Comment
Oof!