国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

解決redis與Python交互取出來(lái)的是bytes類(lèi)型的問(wèn)題

瀏覽:101日期: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)

運(yùn)行結(jié)果:

True

b’python’

這里我們存進(jìn)去的是字符串類(lèi)型的數(shù)據(jù),取出來(lái)卻是字節(jié)類(lèi)型的,這是由于python3的與redis交互的驅(qū)動(dòng)的問(wèn)題,Python2取出來(lái)的就是字符串類(lèi)型的。

為了得到字符串類(lèi)型的數(shù)據(jù),你可以每次取出來(lái)decode一下,但是太繁瑣了,可以這樣設(shè)置:

sr = StrictRedis(host=’localhost’, port=6379, db=0,decode_responses=True)

即在連接數(shù)據(jù)庫(kù)的時(shí)候加上decode_responses=True即可

補(bǔ)充知識(shí):python讀并寫(xiě)入redis 使用pipline管道

日常開(kāi)發(fā)中,我們總是需要將一些文件寫(xiě)入到緩存中。而讀文件較快的方式就是python了,另外python提供了非常好用的api幫助我們連接redis。本例中將會(huì)用rediscluster包來(lái)連接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) # 當(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]# 第一個(gè)參數(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í)候需要將要插入的文件以參數(shù)形式傳入到命令中

例如,將 /data/a.log 插入到redis中

python RedisFIleToRedis.py /data/a.log

以上這篇解決redis與Python交互取出來(lái)的是bytes類(lèi)型的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 长丰县| 霍州市| 和顺县| 龙岩市| 平阳县| 海淀区| 乌鲁木齐市| 泰顺县| 东源县| 高碑店市| 贵定县| 海安县| 南城县| 澎湖县| 磴口县| 崇明县| 左云县| 吉水县| 平罗县| 互助| 顺昌县| 托里县| 黄冈市| 湾仔区| 新密市| 永昌县| 峡江县| 汨罗市| 承德市| 丹阳市| 湘乡市| 丹寨县| 禄劝| 故城县| 玉屏| 六枝特区| 德钦县| 吴川市| 通城县| 奎屯市| 丽江市|