赞
踩
loads(param)是将文本字符串转换为json对象的函数,其函数名是load string 的缩写,意思是加载字符串。所以其参数param必须要是一个字典型的字符串。且字典的键必须用双引号来包裹。
如果是嵌套列表的话,不是str,会报错
TypeError: the JSON object must be str, bytes or bytearray, not 'dict’解决办法
dumps(param)是将json数据对象转换为文本字符串的函数,其函数名是dump string 的缩写,意思是输出字符串,所以其参数param必须要是json对象,也就是loads()函数返回的数据类型。
两个语法
Encode: 把普通字符串 转为 机器可识别的bytes
Decode: 把bytes转为字符串
两个差异
Python3的str 默认不是bytes,所以不能decode,只能先encode转为bytes,再decode
python2的str 默认是bytes,所以能decode
所以str.decode 本质是bytes类型的str的decode
python3经常出现 AttributeError: ‘str’ object has no attribute ‘decode’
非要这样玩,只能先encode转为bytes,再decode
强制转换忽略错误:
bytes.decode(‘’utf-8‘’, ‘’ignore‘’)
记忆小技巧
编码就是encode,把你认识的转为,机器人认识的
解码decode,就是吧一堆机器认识的,解释为人能读懂的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。