python与redis数据之间转化有两种形式:
一:利用pickle模块:
# lucas210303操作redis test
import pickle as pk
import redis
# 连接redis
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
red = redis.StrictRedis(connection_pool=pool)
mydict = {1: 2, 2: 3, 3: 4}
p_mydict = pk.dumps(mydict)
# 写操作
red.set('mydict', p_mydict)
# 读操作
read_dict = red.get('mydict')
yourdict = pk.loads(read_dict)
print(yourdict)
二:利用json模块:
# lucas210303操作redis test
import json as js
import redis
# 连接redis
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
red = redis.StrictRedis(connection_pool=pool)
mydict = {1: 2, 2: 3, 3: 4}
p_mydict = js.dumps(mydict)
# 写操作
red.set('mydict', p_mydict)
# 读操作
read_dict = red.get('mydict')
yourdict = js.loads(read_dict)
print(yourdict)