Convert single quotes to double quotes for Dictionary key/value pair
<p>I have a dictionory with key value pair in single inverted commas as follows:</p>

<pre><code>filename = 'sub-310621_task-EMOTION_acq-LR_bold.nii.gz'
intended_for ={"IntendedFor", filename}
</code></pre>

<p>AS i am want to write this dictionary to a json file i have to have filename in between two inverted commas eg: <code>"sub-310621_task-EMOTION_acq-LR_bold.nii.gz"</code></p>

<p>SO the output should look like:</p>

<pre><code>intended_for ={"IntendedFor", "sub-310621_task-EMOTION_acq-LR_bold.nii.gz"}
</code></pre>

<p>This output will be written in to test.json file which should look like:</p>

<pre><code>{
    "IntendedFor": "sub-310621_task-EMOTION_acq-LR_bold.nii.gz"
}
</code></pre>

<p>How can i do this in python ?</p>

<p>The apostrophes or quotation marks on the ends of a string literal are not included in the string - <code>'asdf'</code> and <code>"asdf"</code> represent identical strings. Neither the key nor the value in your dict actually include the character <code>'</code> or <code>"</code>, so you don't need to perform a conversion.</p>

<p>When you dump the dict to JSON, your JSON dumper will automatically surround strings with <code>"</code> characters, among other necessary escaping. For example, if you're using the <code>json</code> module, you can just do</p>

<pre><code>json_string = json.dumps(intended_for)
</code></pre>

<p>to produce a correctly-formatted JSON string. (If this does not work, you have some other bug you're not showing us.)</p>

python json dictionary