I'm given some input that I must parse and convert to a Dict
. I don't control how the input is generated.
An example input is u'{u\'my_key\': u\'AB\\N\'}'
. Notice the this should represent a serialized dictionary.
Parsing this dictionary string fails using a variety of methods. Using json.loads
fails due to the structure of the string being malformed due to the nested u
. Using ast.literal_eval
fails with a (unicode error) 'unicodeescape' codec can't decode bytes in position 3-4: malformed \N character escape
error.
I need to somehow sanitize the input so the \N
won't be considered an ascii
character when parsed with ast
. Doing a simple replace('\\', '\\\\')
seems error prone and probably has many edge cases.
Alternatively, I need a way to remove the u
from the nested string so json.loads
would work.
Thanks