简体   繁体   中英

python3 redis-py how to auto parse hgetall result?

import redis

config=redis.Redis(host='localhost')
dic={'name':'tom','age':20,'subjects':['eng','cn']}
config.hmset('person',dic)

print(config.hgetall('person')) 

will get {b'age': b'20', b'name': b'tom', b'subjects': b"['eng', 'cn']"} . But I want to get dic object back. namely: {'name':'tom','age':20,'subjects':['eng','cn']} , how?

Add decode_responses=True to Redis() argument.

import redis

config=redis.Redis(host='localhost', decode_responses=True)
dic={'name':'tom','age':20,'subjects':['eng','cn']}
config.hmset('person',dic)

print(config.hgetall('person')) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM