python redis存入字典序列化存儲教程
在python中通過redis hset存儲字典時(shí),必須主動(dòng)把字典通過json.dumps()序列化為字符串后再存儲,
不然hget獲取后將無法通過json.loads()反序列化為字典
序列化存儲
r = redis_conn() r.hset(’wait_task’, ’one’, json.dumps({’project’: ’india’, ’total_size’: ’15.8 MB’})) r.hset(’wait_task’, ’two’, json.dumps({’project’: ’india’, ’total_size’: ’15.8 MB’})) r.hset(’wait_task’, ’three’, json.dumps({’project’: ’india’, ’total_size’: ’15.8 MB’}))
反序列化讀取
for k in r.hkeys(’wait_task’): d = r.hget(’wait_task’, k) print(json.loads(d))
輸出
{’project’: ’india’, ’total_size’: ’15.8 MB’}{’project’: ’india’, ’total_size’: ’15.8 MB’}{’project’: ’india’, ’total_size’: ’15.8 MB’}
補(bǔ)充知識:python redis 存string 取 string
看代碼吧~
DB_REDIS = { ’host’: localhost, ’port’: 6379, ’password’: ’pwd&&1’, ’db’: 1, ’decode_responses’: True}
python3使用時(shí),給客戶端配置’decode_responses’: True
就能保證存取的都是string,而不是想存string,結(jié)果卻是bytes!!!
以上這篇python redis存入字典序列化存儲教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 使用css實(shí)現(xiàn)全兼容tooltip提示框2. SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導(dǎo)出,在線文件導(dǎo)出)3. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果4. 通過工廠模式返回Spring Bean方法解析5. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)6. 關(guān)于Mysql-connector-java驅(qū)動(dòng)版本問題總結(jié)7. python中HTMLParser模塊知識點(diǎn)總結(jié)8. CSS自定義滾動(dòng)條樣式案例詳解9. python 批量下載bilibili視頻的gui程序10. python:刪除離群值操作(每一行為一類數(shù)據(jù))
