python 3.5中的json.loads和Redis [英] json.loads and Redis in python 3.5

查看:121
本文介绍了python 3.5中的json.loads和Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用json.dumps()和RPUSH(ed)在redis列表中创建了一个JSON对象. 当使用LRANGE(redis.lrange())返回JSON时,我收到一个二进制字符串

I created a JSON object with json.dumps() and RPUSH(ed) it in a redis list. When getting back the JSON with LRANGE ( redis.lrange() ) I receive a binary string

 b'{"si":"00:ff" ...

因此json.loads()引发错误: *** TypeError:JSON对象必须为str,而不是"bytes" 我应该如何恢复为ascii?

So json.loads() raises an error: *** TypeError: the JSON object must be str, not 'bytes' How should I revert to ascii ?

推荐答案

通常,您要记住首字母缩略词BADTIE:

In general you want to remember the acronym BADTIE:

Bytes
Are
Decoded
Text
Is
Encoded

如果有字节,请运行my_bytes.decode()来获取文本.

If you have bytes, you run my_bytes.decode() to get text.

如果有文本,请运行my_text.encode()以获取字节.您也可以指定编码(如果知道的话),但是它有一个合理的默认值.

If you have text, you run my_text.encode() to get bytes. You can also specify the encoding if you know it, but it has a sensible default.

这篇关于python 3.5中的json.loads和Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆