Why ast.literal_eval does not work properly and how to fix it?

  data-structures, pandas, python

Suppose a .csv file which looks like this:

  • title: is the name of the column
  • and [senior innovation manager] is the first row.

Note: both strings (title and row) look exactly as written here.

# title    
# [senior innovation manager]

The idea is to convert this list string representation to an actual python list:

import ast
import pandas as pd
import numpy as np

# read the file
df = pd.read_csv(file_path, sep=',', na_values='NA', encoding='latin-1')

# convert first row to actual python list
df['title'][0]=ast.literal_eval(df['title'][0])

# inspect if ast.literal_eval() converted to actual list:
print(df['title'][0])
print(type(df['title'][0]))

However when tried the above code the next error arises:

Traceback (most recent call last):
  File "file_path", line 76, in <module>        
    df['title'][0]=ast.literal_eval(df['title'][0])
  File "C:UsersidAnaconda3libast.py", line 46, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "C:UsersidAnaconda3libast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
    [senior innovation manager]

What’s the nature of this error?

Is it possible to convert this list string representation
to an actual python list?

Source: Python Questions

LEAVE A COMMENT