int
支持转换为 int 类型的,仅有 float、str、bytes,其他类型均不支持。
需要Python3的教程私信回复我【01】即可获取视频教程
1.float -> int
会去掉小数点及后面的数值,仅保留整数部分。
![142052275_2_20180822052447864 142052275_2_20180822052447864](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_2_20180822052447864.jpg)
2.str -> int
如果字符串中有数字(0-9)和正负号(+/-)以外的字符,就会报错。
![142052275_3_20180822052447911 142052275_3_20180822052447911](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_3_20180822052447911.jpg)
3.bytes -> int
如果 bytes 中有数字(0-9)和正负号(+/-)以外的字符,就会报错。
![142052275_4_20180822052447989 142052275_4_20180822052447989](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_4_20180822052447989.jpg)
float
支持转换为 float 类型的,仅有 int、str、bytes,其他类型均不支持。
1.int -> float
int 转换为 float 时,会自动给添加一位小数。
![142052275_5_2018082205244851 142052275_5_2018082205244851](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_5_2018082205244851.jpg)
2.str -> float
如果字符串含有正负号(+/-)、数字(0-9)和小数点(.)以外的字符,则不支持转换。
![142052275_6_20180822052448145 142052275_6_20180822052448145](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_6_20180822052448145.jpg)
3.bytes -> float
如果 bytes 中含有正负号(+/-)、数字(0-9)和小数点(.)以外的字符,则不支持转换。
![142052275_7_20180822052448223 142052275_7_20180822052448223](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_7_20180822052448223.jpg)
complex
仅支持 int、float、str 转换成 complex 类型。
1.int -> complex
int 转换 complex 时,会自动添加虚数部分并以0j表示。
![142052275_8_20180822052448270 142052275_8_20180822052448270](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_8_20180822052448270.jpg)
2.float -> complex
float 转换 complex 时,会自动添加虚数部分并以0j表示。
![142052275_9_20180822052448317 142052275_9_20180822052448317](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_9_20180822052448317.jpg)
3.str -> complex
str 转换 complex 时,如果能转换成 int 或 float,则会转换后再转为 complex。
如果字符串完全符合 complex 表达式规则,也可以转换为 complex 类型值。
![142052275_10_20180822052448379 142052275_10_20180822052448379](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_10_20180822052448379.jpg)
str
str() 函数可以将任意对象转换为字符串。
1.int -> str
int 转换 str 会直接完全转换。
![142052275_11_20180822052448458 142052275_11_20180822052448458](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_11_20180822052448458.jpg)
2.float -> str
float 转换 str 会去除末位为 0 的小数部分。
![142052275_12_20180822052448504 142052275_12_20180822052448504](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_12_20180822052448504.jpg)
3.complex -> str
complex 转换 str,会先将值转化为标准的 complex 表达式,然后再转换为字符串。
![142052275_13_20180822052448567 142052275_13_20180822052448567](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_13_20180822052448567.jpg)
4.bytes -> str
bytes 和 str 的转换比较特殊点,在 Python 3.x 中,字符串和字节不再混淆,而是完全不同的数据类型。
转换为可执行的表达式字符串:
![142052275_14_20180822052448614 142052275_14_20180822052448614](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_14_20180822052448614.jpg)
str() 函数指定 encoding 参数
或者使用 bytes.decode() 方法
可以作实际数据的转换:
![142052275_15_20180822052448676 142052275_15_20180822052448676](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_15_20180822052448676.jpg)
5.list -> str
会先将值格式化为标准的 list 表达式,然后再转换为字符串。
![142052275_16_20180822052448739 142052275_16_20180822052448739](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_16_20180822052448739.jpg)
6.tuple -> str
会先将值格式化为标准的 tuple 表达式,然后再转换为字符串。
![142052275_17_20180822052448786 142052275_17_20180822052448786](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_17_20180822052448786.jpg)
7.dict -> str
会先将值格式化为标准的 dict 表达式,然后再转换为字符串。
![142052275_18_20180822052448848 142052275_18_20180822052448848](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_18_20180822052448848.jpg)
8.set -> str
会先将值格式化为标准的 set 表达式,然后再转换为字符串。
![142052275_19_20180822052448895 142052275_19_20180822052448895](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_19_20180822052448895.jpg)
9.其他类型
转换内置对象:
![142052275_20_20180822052448958 142052275_20_20180822052448958](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_20_20180822052448958.jpg)
转换类实例:
![142052275_21_2018082205244936 142052275_21_2018082205244936](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_21_2018082205244936.jpg)
转换函数:
![142052275_22_20180822052449114 142052275_22_20180822052449114](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_22_20180822052449114.jpg)
bytes
仅支持 str 转换为 bytes 类型。
![142052275_23_20180822052449145 142052275_23_20180822052449145](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_23_20180822052449145.jpg)
list
支持转换为 list 的类型,只能是序列,比如:str、tuple、dict、set等。
1.str -> list
![142052275_24_20180822052449270 142052275_24_20180822052449270](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_24_20180822052449270.jpg)
2.bytes -> listbytes
转换列表,会取每个字节的 ASCII 十进制值并组合成列表
![142052275_25_20180822052449332 142052275_25_20180822052449332](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_25_20180822052449332.jpg)
3.tuple -> list
tuple 转换为 list 比较简单。
![142052275_26_20180822052449379 142052275_26_20180822052449379](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_26_20180822052449379.jpg)
4.dict -> list
字典转换列表,会取键名作为列表的值。
![142052275_27_20180822052449426 142052275_27_20180822052449426](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_27_20180822052449426.jpg)
5.set -> list
集合转换列表,会先去重为标准的集合数值,然后再转换。
![142052275_28_20180822052449473 142052275_28_20180822052449473](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_28_20180822052449473.jpg)
tuple
与列表一样,支持转换为 tuple 的类型,只能是序列。
1.str -> tuple
![142052275_29_20180822052449520 142052275_29_20180822052449520](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_29_20180822052449520.jpg)
2.bytes -> tuple
bytes 转换元组,会取每个字节的 ASCII 十进制值并组合成列表。
![142052275_30_20180822052449551 142052275_30_20180822052449551](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_30_20180822052449551.jpg)
3.list -> tuple
![142052275_31_20180822052449614 142052275_31_20180822052449614](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_31_20180822052449614.jpg)
4.dict -> tuple
![142052275_32_20180822052449661 142052275_32_20180822052449661](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_32_20180822052449661.jpg)
5.set -> tuple
![142052275_33_20180822052449723 142052275_33_20180822052449723](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_33_20180822052449723.jpg)
dict
1.str -> dict
- 使用 json 模块
使用 json 模块转换 JSON 字符串为字典时,需要求完全符合 JSON 规范
尤其注意键和值只能由单引号包裹,否则会报错。
![142052275_34_20180822052449754 142052275_34_20180822052449754](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_34_20180822052449754.jpg)
- 使用 eval 函数
因为 eval 函数能执行任何符合语法的表达式字符串
所以存在严重的安全问题,不建议。
- 使用 ast.literal_eval 方法
使用 ast.literal_eval 进行转换既不存在使用 json 进行转换的问题
也不存在使用 eval 进行转换的 安全性问题,因此推荐使用 ast.literal_eval。
![142052275_35_20180822052449801 142052275_35_20180822052449801](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_35_20180822052449801.jpg)
2.list -> dict
通过 zip 将 2 个列表映射为字典:
![142052275_36_20180822052449864 142052275_36_20180822052449864](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_36_20180822052449864.jpg)
将嵌套的列表转换为字典:tuple -> dict
通过 zip 将 2 个元组映射为字典:
![142052275_37_20180822052449895 142052275_37_20180822052449895](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_37_20180822052449895.jpg)
将嵌套的元组转换为字典:
![142052275_38_20180822052449958 142052275_38_20180822052449958](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_38_20180822052449958.jpg)
3.set -> dict
通过 zip 将 2 个集合映射为字典:
![142052275_39_2018082205245020 142052275_39_2018082205245020](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_39_2018082205245020.jpg)
set
1.str -> set
先将字符切割成元组,然后再去重转换为集合。
![142052275_40_2018082205245083 142052275_40_2018082205245083](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_40_2018082205245083.jpg)
2.bytes -> set
会取每个字节的 ASCII 十进制值并组合成元组,再去重。
![142052275_41_20180822052450114 142052275_41_20180822052450114](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_41_20180822052450114.jpg)
3.list -> set
先对列表去重,再转换。
![142052275_42_20180822052450176 142052275_42_20180822052450176](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_42_20180822052450176.jpg)
4.tuple -> set
先对列表去重,再转换。
![142052275_43_20180822052450223 142052275_43_20180822052450223](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_43_20180822052450223.jpg)
5.dict -> set
会取字典的键名组合成集合。
![142052275_44_20180822052450286 142052275_44_20180822052450286](https://www.cnraksmart.com/wp-content/uploads/2023/01/142052275_44_20180822052450286.jpg)