用python操作redis存储中文后,再取出的数据变成了乱码怎么办?
redis_config = {
"host": "192.168.3.6",
"port": 6379,
"db": 0
}
pool = redis.ConnectionPool(host=redis_config["host"],
port=redis_config["port"],
db=redis_config["db"])
r = redis.Redis(connection_pool=pool,charset='UTF-8',decode_responses=True,encoding='UTF-8')
m = "哈哈哈"
r.hset("hash3", "title", m)
x = r.hmget("hash1", "title")
print(x)
[b'\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88']
需要decode一下
import redis
redis_config = {
"host": "127.0.0.1",
"port": 6379,
"db": 0
}
pool = redis.ConnectionPool(host=redis_config["host"],
port=redis_config["port"],
db=redis_config["db"])
r = redis.Redis(connection_pool=pool, charset='utf-8', decode_responses=True)
m = "哈哈哈"
r.hset("hash3", "title", m)
x = r.hmget("hash3", "title")
print(x[0].decode("utf-8"))
没有乱码,只是没有格式化好输出而已。
解码一下就可以了,希望对你有帮助
a = b'\xe5\x93\x88\xe5\x93\x88\xe5\x93\x88'
print(a.decode('utf-8'))
参考到这个方式方法,里面讲解很多格式转换。链接:https://www.cnblogs.com/doudongchun/p/3693784.html