Python decode()方法

来自linux中国网wiki
Evan讨论 | 贡献2021年7月11日 (日) 14:55的版本
跳到导航 跳到搜索

Python decode()方法以 encoding 指定的编码格式解码字符串。默认编码为字符串编码

encode() 方法为字符串类型(str)提供的方法,用于将 str 类型转换成 bytes 类型,这个过程也称为“编码”。

例子

In [38]: dic["温度"] = items[0][3]

In [39]: dic["温度"]
Out[39]: '17 ~ 27\xe2\x84\x83'

In [42]: print (dic["温度"].decode('utf-8'))
17 ~ 27℃

In [50]:  print (dic["天气"].decode(encoding='utf-8'))
晴

参考

Python 解码字符串 decode()方法

Python 编码字符串encode()方法