解决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
投稿

猜你喜欢

  • PHP实现PDF转图片的详细过程(使用imagick)

    2023-09-07 13:27:34
  • Python使用py2exe打包程序介绍

    2023-11-12 21:29:55
  • Python DPED机器学习之实现照片美化

    2023-11-25 01:50:24
  • 利用python3如何给数据添加高斯噪声

    2023-06-11 20:52:17
  • Python多进程fork()函数详解

    2023-06-08 19:41:37
  • 用Python将Excel数据导入到SQL Server的例子

    2021-08-21 19:24:30
  • Python实现输出某区间范围内全部素数的方法

    2022-04-25 10:04:50
  • 使用TensorFlow实现简单线性回归模型

    2022-11-30 19:51:48
  • 使用Gitee自动化部署python脚本的详细过程

    2022-03-30 07:04:55
  • python输入、数据类型转换及运算符方式

    2021-08-09 19:20:17
  • Python中的正则表达式与JSON数据交换格式

    2023-06-21 18:16:26
  • python和node.js生成当前时间戳的示例

    2022-05-26 05:07:43
  • python的函数最详解

    2022-02-15 17:05:05
  • Python排序算法之插入排序及其优化方案详解

    2021-04-03 05:39:31
  • Python基础语法之变量与数据类型详解

    2022-06-21 17:52:36
  • asp长文章分页显示思路

    2007-08-23 13:54:00
  • 详解Python中的from..import绝对导入语句

    2022-08-10 10:01:33
  • 浅谈LogMiner的使用方法

    2009-02-28 11:12:00
  • MySQL错误中文参照列表

    2010-09-30 14:41:00
  • python实现逆滤波与维纳滤波示例

    2023-07-01 16:36:25
  • asp之家 网络编程 m.aspxhome.com