[python] String to list easy to use function sharing

In daily use, pandas is often used to read table data. For example, if the information we need is in json format or list format, pandas will read it in the form of a string. After consulting, it is found that the method of converting strings to lists is the function list(), but this cannot solve practical problems, as shown in the figure below

Use the list function to separate all the contents and store them in the form of strings.

Here share an ast package to solve this problem

import ast
i='{"name":"shuaige"}'
print(type(i))
ii=ast.literal_eval(i)
print(type(ii))
print(ii['name'])

result:

<class 'str'>

<class 'dict'>

shuaige

In this way, this requirement can be solved by converting strings in formats such as lists, tuples, and dictionaries into corresponding types.

Guess you like

Origin blog.csdn.net/qq_44992785/article/details/129302055