json.loads and Redis in python 3.5

落花浮王杯 提交于 2019-12-06 11:03:31

问题


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" ...

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


回答1:


In general you want to remember the acronym BADTIE:

Bytes
Are
Decoded
Text
Is
Encoded

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

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.



来源:https://stackoverflow.com/questions/41006093/json-loads-and-redis-in-python-3-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!