解决redis与Python交互取出来的是bytes类型的问题
作者:重装大师1024 时间:2021-04-26 20:54:08
基本代码
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-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) # 当前目录路径
# 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
来源:https://blog.csdn.net/weixin_40612082/article/details/83958864
标签:redis,Python,bytes
0
投稿
猜你喜欢
vue中v-for通过动态绑定class实现触发效果
2024-04-09 10:45:21
python神经网络MobileNet模型的复现详解
2022-09-24 19:54:14
python使用rsa非对称加密过程解析
2021-06-15 00:14:51
详解mysql数据去重的三种方式
2024-01-22 03:06:35
Python使用matplotlib绘制三维图形示例
2022-09-14 10:53:14
Python正则表达式的另类解答
2023-08-02 06:58:04
JS严格模式知识点总结
2024-05-13 10:06:42
Mysql巧用join优化sql的方法详解
2024-01-16 01:19:34
Python+Opencv实现数字识别的示例代码
2021-03-10 14:16:38
Python 获取当前所在目录的方法详解
2021-07-19 09:41:14
关于搜索建议的两点小问题
2011-09-16 20:15:29
SQL对时间处理的语句小结
2011-12-01 07:53:04
使用python实现ANN
2022-05-30 09:56:42
scrapy利用selenium爬取豆瓣阅读的全步骤
2022-09-24 01:28:15
ASP利用TCPIP.DNS组件获得域名对应的IP
2009-11-07 19:21:00
利用pandas进行大文件计数处理的方法
2021-05-20 02:36:55
Oracle9i取得建表和索引的DDL语句
2010-07-20 12:59:00
使用pandas对两个dataframe进行join的实例
2022-02-06 11:09:36
教你用Python脚本快速为iOS10生成图标和截屏
2022-03-04 05:08:28
C#连接Oracle数据库字符串(引入DLL)的方式
2024-01-25 16:10:07