Hangfish Hangfish - 2 months ago 17
Python Question

String/dict manipulation ' to "

{'coolstring': {'stepone': {'X': '44', 'Y': '66'}}}

this is a string/dictionary that I create with input(), however, I need to change all the ' or Apostrophes to " or Quotation marks. The reasoning for this is that I need to do so for it be properly passed as a Json string further down my pipeline.

My idea is using .replace() but I'm not quite sure how to format it so that it swaps ' with " any tips?


If your input is a dictionary, there's a json module which does exactly what you need:

import json

dic = {'coolstring': {'stepone': {'X': '44', 'Y': '66'}}}

json_string = json.dumps(dic)

The output will be:

{"coolstring": {"stepone": {"X": "44", "Y": "66"}}}

However if your data is a string, you can use the proposed solution from the other comment by using replace()