python3 导出redis数据,根据redis前缀模糊匹配

python3 导出redis数据,根据redis前缀模糊匹配

# -*- coding: utf-8 -*-import redis
import json# 连接Redis
r = redis.Redis(host='you host ip addr',port=6379,password='you password',decode_responses=True
)# 查询数据
pattern = 'test:cmsServer:article:*'
keys = r.keys(pattern)# 导出数据
export_data = {}
for key in keys:value = r.get(key)export_data[key] = value# 保存到文件
with open('redis_export.json', 'w', encoding='utf-8') as f:json.dump(export_data, f, ensure_ascii=False, indent=2)print("导出完成,共{}条数据".format(len(keys)))