Loading string representation of dictionaries with single quotes in their values as dictionary literals

Solution 1:

In the event that you need to escape those ' characters, I think the re module might help.

import ast
import re

data = "{'name' : 'D'Artagnan'}"
data = re.sub(r"(\w)'(\w)", r"\1\\'\2", data)
print(ast.literal_eval(data))

Should give you a dictionary:

{'name': "D'Artagnan"}