国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

解決redis與Python交互取出來的是bytes類型的問題

瀏覽:75日期:2022-07-17 11:35:51

基本代碼

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’, port=6379, db=0,decode_responses=True)

即在連接數據庫的時候加上decode_responses=True即可

補充知識:python讀并寫入redis 使用pipline管道

日常開發中,我們總是需要將一些文件寫入到緩存中。而讀文件較快的方式就是python了,另外python提供了非常好用的api幫助我們連接redis。本例中將會用rediscluster包來連接redis集群,并使用pipeline管道插入文件

# encoding: utf-8from rediscluster import StrictRedisClusterimport sysimport osimport 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:datalogshippo.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類型的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 制服丝袜怡红院 | 韩国免费毛片 | 日韩视频久久 | 国产高清一区 | 怡红院自拍 | 爱久久精品国产 | 亚洲一区二区三区久久久久 | a级毛片在线视频免费观看 a级免费 | 国产欧美一区二区三区在线看 | 韩国精品一区视频在线播放 | 中文字幕亚洲欧美日韩不卡 | 国产成人小视频在线观看 | 久久成人免费播放网站 | 99久久精品费精品国产一区二 | 欧美在线播放成人免费 | 日本69色视频在线观看 | 国内精品2020情侣视频 | 日本三级免费网站 | 中国一级特黄大片毛片 | 中国人免费观看高清在线观看二区 | 这里只有久久精品视频 | 男女在线视频 | 女人张开腿给人桶免费视频 | 在线观看香蕉免费啪在线观看 | 免费观看日本高清a毛片 | 一级日韩 | 亚洲视频精品在线观看 | 免费看欧美日韩一区二区三区 | 国内精品免费一区二区三区 | 亚洲香蕉影院 | 国产三级小视频在线观看 | 国产成人一区二区三区免费观看 | 国产一区二区三区在线观看精品 | 成年人视频在线免费播放 | 亚洲美女视频网站 | 看性过程三级视频在线观看 | 91网站网站网站在线 | 亚洲精品一区二区三区四区 | 成人精品久久 | 久久91精品国产91久久 | 日韩一级欧美一级毛片在线 |