解決redis與Python互動取出來的是bytes型別的問題
阿新 • • 發佈:2020-07-21
基本程式碼
from redis import * if __name__ == '__main__': sr = StrictRedis(host='localhost',port=6379,db=0) result=sr.set('name','python') print(result) result1 = sr.get('name') print(result1)
執行結果:
True
b'python'
這裡我們存進去的是字串型別的資料,取出來卻是位元組型別的,這是由於python3的與redis互動的驅動的問題,Python2取出來的就是字串型別的。
為了得到字串型別的資料,你可以每次取出來decode一下,但是太繁瑣了,可以這樣設定:
sr = StrictRedis(host='localhost',db=0,decode_responses=True)
即在連線資料庫的時候加上decode_responses=True即可
補充知識:python讀並寫入redis 使用pipline管道
日常開發中,我們總是需要將一些檔案寫入到快取中。而讀檔案較快的方式就是python了,另外python提供了非常好用的api幫助我們連線redis。本例中將會用rediscluster包來連線redis叢集,並使用pipeline管道插入檔案
# encoding: utf-8 from rediscluster import StrictRedisCluster import sys import os import datetime # redis_nodes = [{"host": "10.80.23.175","port": 7000},# {"host": "10.80.23.175","port": 7001},# {"host": "10.80.24.175",# {"host": "10.80.25.175","port": 7001} # ] def redis_cluster(): redis_nodes = [{"host": "10.80.23.175",{"host": "10.80.23.175",{"host": "10.80.24.175",{"host": "10.80.25.175","port": 7001} ] try: redisconn = StrictRedisCluster(startup_nodes=redis_nodes,skip_full_coverage_check=True) return redisconn except Exception as e: print("Connect Error!") sys.exit(1) def to_redis(redis_conn1,file_name): # file_name = "D:\data\logs\hippo.log" pipe = redis_conn1.pipeline() # pos = [] index = 0 count = 0 with open(file_name,'r') as file_to_read: while True: lines = file_to_read.readline() lines = lines.replace("\n","") if not lines: break pass s = lines.split("\t") value = s[1] key = s[0] result = pipe.lpush(key,value) # print(file_name + s) index = index + 1 if index > 5000: pipe.execute() index = 0 count = count + 1 print("execute insert! count is %d" % count) pass pass pipe.execute() def read_file(path): if os.path.isfile(path): print("start execute file %s" % path) to_redis(path) else: for root,dirs,files in os.walk(path): # print('root_dir:',root) # 當前目錄路徑 # print('sub_dirs:',dirs) # 當前路徑下所有子目錄 print('files:',files) # 當前路徑下所有非目錄子檔案 for fileName in files: all_name = root + "/" + fileName print("start execute file %s" % all_name) to_redis(redis_conn,all_name) start_time = datetime.datetime.now() redis_conn = redis_cluster() file_paths = sys.argv[1] # 第一個引數是本檔案 故去掉 #file_paths.pop[0] #for file_name in file_paths: #print(file_paths) read_file(file_paths) end_time = datetime.datetime.now() print("use times is %d " % (end_time - start_time).seconds)
在使用的時候需要將要插入的檔案以引數形式傳入到命令中
例如,將 /data/a.log 插入到redis中
python RedisFIleToRedis.py /data/a.log
以上這篇解決redis與Python互動取出來的是bytes型別的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。