Home > Article > Backend Development > How to convert string into dictionary
In actual work, you need to convert a python string into a dictionary, such as string:
user_info = '{"name" : "john", "gender" : "male", "age ": 28}'
We want to convert it into the following dictionary:
user_dict = {"name" : "john", "gender" : "male", "age": 28}
There are several methods:
1. Convert
>>> import json >>> user_info= '{"name" : "john", "gender" : "male", "age": 28}' >>> user_dict = json.loads(user_info) >>> user_dict {u'gender': u'male', u'age': 28, u'name': u'john'}
through json but use There is a potential problem with converting to json.
Because json syntax stipulates that strings in arrays or objects must use double quotes, single quotes cannot be used (a description on the official website is "A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes"), so the following conversion is wrong:
>>> import json >>> user_info = "{'name' : 'john', 'gender' : 'male', 'age': 28}" # 由于字符串使用单引号,会导致运行出错 >>> user_dict = json.loads(user_info) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads return _default_decoder.decode(s) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 380, in raw_decode obj, end = self.scan_once(s, idx) ValueError: Expecting property name: line 1 column 2 (char 1)
2. Converting through eval
>>> user_info = '{"name" : "john", "gender" : "male", "age": 28}' >>> user_dict = eval(user_info) >>> user_dict {'gender': 'male', 'age': 28, 'name': 'john'} >>> user_info = "{'name' : 'john', 'gender' : 'male', 'age': 28}" >>> user_dict = eval(user_info) >>> user_dict {'gender': 'male', 'age': 28, 'name': 'john'}
does not exist when using json above. Problem with conversion. However, there are security issues when using eval, such as the following example:
# 让用户输入 `user_info` >>> user_info = raw_input('input user info: ') # 输入 {"name" : "john", "gender" : "male", "age": 28},没问题 >>> user_dict = eval(user_info) # 输入 __import__('os').system('dir'),user_dict 会列出当前的目录文件! # 再输入一些删除命令,则可以把整个目录清空了! >>> user_dict = eval(user_info)
3. Using ast.literal_eval for conversion through literal_eval
>>> import ast >>> user = '{"name" : "john", "gender" : "male", "age": 28}' >>> user_dict = ast.literal_eval(user) >>> user_dict {'gender': 'male', 'age': 28, 'name': 'john'} user_info = "{'name' : 'john', 'gender' : 'male', 'age': 28}" >>> user_dict = ast.literal_eval(user) >>> user_dict {'gender': 'male', 'age': 28, 'name': 'john'}
is neither possible. json conversion, there is no security issue with eval conversion, so it is recommended to use ast.literal_eval.
The above is the detailed content of How to convert string into dictionary. For more information, please follow other related articles on the PHP Chinese website!