I want to get the output with single backslash in my json file which is an input to my javascript/HTML document. it is for visualization purposes using vis.js I am unable to get rid of Python's way of adding a backslash before a backslash. I have tried escaping backslash and using r'' type strings.
I have a dictionary:
f = {'id':0,'group':'main', 'label':'Main', \
'font':{'size': 60}, 'shape': 'icon', 'icon': {'face':'Ionicons',\
'code': r'\uf276', 'size':200, 'color':'#f0a30a'}, \
'color': {'background': 'pink', 'border': 'black'}}
In [91]: print f
'{'group': 'main', 'color': {'border': 'black', 'background': 'pink'}, 'label': 'Main', 'shape': 'icon', 'font': {'size': 60}, 'id': 0, 'icon': {'size': 200, 'color': '#f0a30a', 'code': '\\uf276', 'face': 'Ionicons'}}
I am dumping it into a json file using json.dumps
In [92]: json.dumps(f)
Out[92]: '{"group": "main", "color": {"border": "black", "background": "pink"}, "label": "Main", "shape": "icon", "font": {"size": 60}, "id": 0, "icon": {"size": 200, "color": "#f0a30a", "code": "\\\\uf276", "face": "Ionicons"}}'
In [93]: print json.dumps(f)
{"group": "main", "color": {"border": "black", "background": "pink"}, "label": "Main", "shape": "icon", "font": {"size": 60}, "id": 0, "icon": {"size": 200, "color": "#f0a30a", "code": "\\uf276", "face": "Ionicons"}}
I am saving this string into a file using json.dump(filename, f) and it is outputting this:
{
"group": "main",
"color": {
"border": "black",
"background": "pink"
},
"label": "Main",
"shape": "icon",
"font": {
"size": 60
},
"id": 0,
"icon": {
"size": 200,
"color": "#f0a30a",
"code": "\\uf276",
"face": "Ionicons"
}
}
r'foobar'), which is why it's actually a backslash, not an escape sequence.