解決redis與Python交互取出來的是bytes類型的問題
基本代碼
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)
運(yùn)行結(jié)果:
True
b'python'
這里我們存進(jìn)去的是字符串類型的數(shù)據(jù),取出來卻是字節(jié)類型的,這是由于python3的與redis交互的驅(qū)動的問題,Python2取出來的就是字符串類型的。
為了得到字符串類型的數(shù)據(jù),你可以每次取出來decode一下,但是太繁瑣了,可以這樣設(shè)置:
sr = StrictRedis(host='localhost', port=6379, db=0,decode_responses=True)
即在連接數(shù)據(jù)庫的時候加上decode_responses=True即可
補(bǔ)充知識:python讀并寫入redis 使用pipline管道
日常開發(fā)中,我們總是需要將一些文件寫入到緩存中。而讀文件較快的方式就是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", "port": 7000}, # {"host": "10.80.24.175", "port": 7001}, # {"host": "10.80.25.175", "port": 7000}, # {"host": "10.80.25.175", "port": 7001} # ] def redis_cluster(): redis_nodes = [{"host": "10.80.23.175", "port": 7000}, {"host": "10.80.23.175", "port": 7001}, {"host": "10.80.24.175", "port": 7000}, {"host": "10.80.24.175", "port": 7001}, {"host": "10.80.25.175", "port": 7000}, {"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) # 當(dāng)前目錄路徑 # print('sub_dirs:', dirs) # 當(dāng)前路徑下所有子目錄 print('files:', files) # 當(dāng)前路徑下所有非目錄子文件 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] # 第一個參數(shù)是本文件 故去掉 #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)
在使用的時候需要將要插入的文件以參數(shù)形式傳入到命令中
例如,將 /data/a.log 插入到redis中
python RedisFIleToRedis.py /data/a.log
以上這篇解決redis與Python交互取出來的是bytes類型的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python tkinter模塊彈出窗口及傳值回到主窗口操作詳解
這篇文章主要介紹了Python tkinter模塊彈出窗口及傳值回到主窗口操作,結(jié)合實(shí)例形式分析了Python使用tkinter模塊實(shí)現(xiàn)的彈出窗口及參數(shù)傳遞相關(guān)操作技巧,需要的朋友可以參考下2017-07-07使用python-magic和wxPython實(shí)現(xiàn)識別文檔類型
這篇文章主要介紹了如何使用python-magic模塊和wxPython庫創(chuàng)建一個簡單的文件列表應(yīng)用程序,該應(yīng)用程序可以顯示所選文件夾中文件的類型,需要的可以參考下2023-08-08python manage.py runserver流程解析
這篇文章主要介紹了python manage.py runserver流程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11python matplotlib模塊基本圖形繪制方法小結(jié)【直線,曲線,直方圖,餅圖等】
這篇文章主要介紹了python matplotlib模塊基本圖形繪制方法,結(jié)合實(shí)例形式總結(jié)分析了Python使用matplotlib模塊繪制直線,曲線,直方圖,餅圖等圖形的相關(guān)操作技巧,需要的朋友可以參考下2020-04-04利用Python繪制創(chuàng)意中秋節(jié)月餅
又是一年中秋至——花好月圓夜。turtle庫作為Python重要的標(biāo)準(zhǔn)庫之一,是最有價值的程序設(shè)計入門實(shí)踐庫,它是程序設(shè)計入門層面最常用的基本繪圖庫。本文將使用turtle(海龜)來繪制中秋創(chuàng)意月餅,感興趣的可以了解一下2022-09-09Python 基于FIR實(shí)現(xiàn)Hilbert濾波器求信號包絡(luò)詳解
今天小編就為大家分享一篇Python 基于FIR實(shí)現(xiàn)Hilbert濾波器求信號包絡(luò)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02python+mysql實(shí)現(xiàn)簡單的web程序
上篇文章我們介紹了簡單的Python web程序,實(shí)現(xiàn)hello world,本文我們來結(jié)合一下mysql,實(shí)現(xiàn)對數(shù)據(jù)庫的簡單操作,希望對大家有所幫助2014-09-09