1π
raw_input()
returns a string, which is an entirely different object from inp
which is a Python dictionary.
If you wanted raw_input
to be treated as JSON, first parse it as such:
inp = raw_input("Enter a Dict")
inp = django.utils.simplejson.loads(inp)
django.utils.simplejson.dump(inp, f)
or parse it as a Python literal expression:
from ast import literal_eval
inp = raw_input("Enter a Dict")
inp = literal_eval(inp)
django.utils.simplejson.dump(inp, f)
π€Martijn Pieters
0π
You are converting the userβs input into a string. It is not a dictionary; it is a string that start with {
and ends with }
.
You need to parse/evaluate it as a dictionary first before converting it to json.
>>> inp = "{'a': 1, 'b': 2}"
>>> json.dumps(inp)
'"{\'a\': 1, \'b\': 2}"'
>>> import ast
>>> d = ast.literal_eval(inp)
>>> json.dumps(d)
'{"a": 1, "b": 2}'
π€Burhan Khalid
- [Answer]-Change filename in Django Pisa
- [Answer]-And again Django can't find template (TemplateDoesNotExist)
- [Answer]-Unable to access static files with NGINX, gunicorn and Django
- [Answer]-Decode sessiondata of Django user which is encoded into base64 in Lua
- [Answer]-Django heavy database query and data processing
0π
Input = β{βaβ:1,βbβ:2}β
import ast
ast.literal_eval(Input)
OR
eval(Input)
It will work for youβ¦ π
Output -: {βaβ:1,βbβ:2}
π€Nilesh
Source:stackexchange.com