Home  >  Article  >  Backend Development  >  How to convert string into dictionary

How to convert string into dictionary

anonymity
anonymityOriginal
2019-05-25 11:20:4916284browse

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}

How to convert string into dictionary

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 = &#39;{"name" : "john", "gender" : "male", "age": 28}&#39;
>>> user_dict = eval(user_info)
>>> user_dict
{&#39;gender&#39;: &#39;male&#39;, &#39;age&#39;: 28, &#39;name&#39;: &#39;john&#39;}
>>> user_info = "{&#39;name&#39; : &#39;john&#39;, &#39;gender&#39; : &#39;male&#39;, &#39;age&#39;: 28}"
>>> user_dict = eval(user_info)
>>> user_dict
{&#39;gender&#39;: &#39;male&#39;, &#39;age&#39;: 28, &#39;name&#39;: &#39;john&#39;}

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(&#39;input user info: &#39;)
# 输入 {"name" : "john", "gender" : "male", "age": 28},没问题
>>> user_dict = eval(user_info)
# 输入 __import__(&#39;os&#39;).system(&#39;dir&#39;),user_dict 会列出当前的目录文件!
# 再输入一些删除命令,则可以把整个目录清空了!
>>> user_dict = eval(user_info)

3. Using ast.literal_eval for conversion through literal_eval

>>> import ast
>>> user = &#39;{"name" : "john", "gender" : "male", "age": 28}&#39;
>>> user_dict = ast.literal_eval(user)
>>> user_dict
{&#39;gender&#39;: &#39;male&#39;, &#39;age&#39;: 28, &#39;name&#39;: &#39;john&#39;}
user_info = "{&#39;name&#39; : &#39;john&#39;, &#39;gender&#39; : &#39;male&#39;, &#39;age&#39;: 28}"
>>> user_dict = ast.literal_eval(user)
>>> user_dict
{&#39;gender&#39;: &#39;male&#39;, &#39;age&#39;: 28, &#39;name&#39;: &#39;john&#39;}

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn