Summary data type conversion python

1. int
supports conversion to type int, only float, str, bytes, other types are not supported.

2. float
supports conversion to type float, only int, str, bytes, other types are not supported.

3. complex
only supports int, float, str converted to a complex type.

4. str

str () function can convert any object to a string.

5. The bytes
only supports a bytes type str.

6. The list
supports conversion to the type of list, only a sequence, for example: str, tuple, dict, set the like.

7. tuple
and list the same type of support is converted to the tuple, only sequence.

Dict 8. The
(. 1) is converted to dict STR
A. Use json module
using module json JSON string is converted dictionary, request in full compliance with JSON specification, with particular attention to the key and value can only be wrapped in single quotes, or error.

b. Using the eval function
because the eval function can perform any grammatical expression string, so there is a serious security problem, it is not recommended.

c. Use ast.literal_eval method
using ast.literal_eval be a problem neither use json conversion conversion, there is no security problem using eval conversion, it is recommended to use ast.literal_eval.

(2) list is converted to dict
A by zip list mapped to the plurality of dictionaries;.
. B nested list into dictionaries.

(3) tuple convert dict
A zip plurality of tuples by mapping as a dictionary;.
B.

Guess you like

Origin blog.csdn.net/qq_41490561/article/details/103938594