Search code examples
pythonabstract-syntax-treevalueerror

How to interpret ast.literal_eval() ValueError message?


When attempting to use ast.literal_eval() on an extremely massive string I receive the error ValueError: malformed node or string on line 1: <ast.Name object at 0x000001FAAA9B1420>. It is prohibitively time consuming to go through the entire string looking for where ast.literal_eval() found an item of a type it can't handle. The error message seems to give a location for where it errored as some sort of hex value, but how do I interpret 0x000001FAAA9B1420 to see where this is in the input string?

How would I see the index in the string at which ast.literal_eval() errored by referencing the ValueError error message? Is there any way to see this index otherwise?


Solution

  • According to @jasonharper's comment, this is the solution that worked for me:

    parsedString = ast.parse(reallyLongString)
    for node in ast.walk(parsedString):
         if isinstance(node, ast.Name): #Detects variable names
              print(reallyLongString[node.col_offset - 5:node.col_offset + 5])
    

    This prints the parts of the string that will error out ast.literal_eval. Turns out my problem was that an API response was giving me non-Python values instead of None, True, and False. So the end solution was:

    reallyLongString = reallyLongString.replace('null', 'None')
    reallyLongString = reallyLongString.replace('true', 'True')
    reallyLongString = reallyLongString.replace('false', 'False')
    reallyLongDict = ast.literal_eval(reallyLongString)