I am trying to create a python dictionary which is to be used as a java script var inside a html file for visualization purposes. As a requisite, I am in need of creating the dictionary with all names inside double quotes instead of default single quotes which Python uses. Is there an easy and elegant way to achieve this.
couples = [
['jack', 'ilena'],
['arun', 'maya'],
['hari', 'aradhana'],
['bill', 'samantha']]
pairs = dict[couples]
print pairs
Generated Output:
{'arun': 'maya', 'bill': 'samantha', 'jack': 'ilena', 'hari': 'aradhana'}
Expected Output:
{"arun": "maya", "bill": "samantha", "jack": "ilena", "hari": "aradhana"}
I know, json.dumps[pairs]
does the job, but the
dictionary as a whole is converted into a string which isn't what I am expecting.
P.S.: Is there an alternate way to do this with using json, since I am dealing with nested dictionaries.
I'm sure you can and I'm sure it involves hacking away at some C Internals. It's just how Python represents strings when the string doesn't contain any single quotes inside of it.
strs = ["Fred is dead", '"Hello," she said', "Fred's head.", 'Fred meets Ned.']
print[strs]
>>> ['Fred is dead', '"Hello," she said', "Fred's head", 'Fred meets Ned.']
The only one that isn't single quoted is the one that actually contains an apostrophe. However, unlike say PHP, single and double quotes have no difference.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given dictionary with string keys, remove double quotes from it.
Input : test_dict = {‘”Geeks”‘ : 3, ‘”g”eeks’ : 9}
Output : {‘Geeks’: 3, ‘geeks’: 9}
Explanation : Double quotes removed from keys.Input : test_dict = {‘”Geeks”‘ : 3}
Output : {‘Geeks’: 3}
Explanation : Double quotes removed from keys.
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
test_dict
=
{
'"Geeks"'
:
3
,
'"is" for'
:
5
,
'"g"eeks'
:
9
}
print
[
"The original dictionary is : "
+
str
[test_dict]]
res
=
{key.replace[
'"'
, '']:val
for
key, val
in
test_dict.items[]}
print
[
"The dictionary after removal of double quotes : "
+
str
[res]]
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
import
re
test_dict
=
{
'"Geeks"'
:
3
,
'"is" for'
:
5
,
'"g"eeks'
:
9
}
print
[
"The original dictionary is : "
+
str
[test_dict]]
res
=
{re.sub[r
'"'
, '', key]: val
for
key, val
in
test_dict.items[]}
print
[
"The dictionary after removal of double quotes : "
+
str
[res]]
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}