I need to escape double quotes when converting a dict to json in Python, but I'm struggling to figure out how. Show So if I have a dict like
This seems like such a simple problem to solve but I'm really struggling with it. asked Jul 24, 2018 at 17:04
8 Your last attempt The reason you see this:
Is because
you're printing the representation of the string. The string itself will have only a single backslash for each quote. If you use answered Jul 24, 2018 at 17:21
nosklonosklo 209k55 gold badges290 silver badges295 bronze badges What you have does work. Python is showing you the literal representation of it. If you save it to a variable and print it shows you what you're looking for.
answered Jul 24, 2018 at 17:23
View Discussion Improve Article Save Article View Discussion Improve Article Save Article Given dictionary with string keys, remove double quotes from it.
Method #1 : Using dictionary comprehension + replace() The combination of above functionalities can be used to solve this problem. In this, we perform removal of double quotes using replace() with empty string. The dictionary comprehension is used for remaking dictionary. Python3
Output The original dictionary is : {'"Geeks"': 3, '"is" for': 5, '"g"eeks': 9} The dictionary after removal of double quotes : {'Geeks': 3, 'is for': 5, 'geeks': 9} Method #2 : Using re.sub() + dictionary comprehension The combination of above functions is also an alternative to solve this task. In this, we employ regex to solve the problem. Python3
Output The original dictionary is : {'"Geeks"': 3, '"is" for': 5, '"g"eeks': 9} The dictionary after removal of double quotes : {'Geeks': 3, 'is for': 5, 'geeks': 9} |