当前位置: 首页 > news >正文

口碑好的网页设计服务武汉网站seo

口碑好的网页设计服务,武汉网站seo,南阳疫情防控最新通知,网站备案信息如何注销上一篇:Redis:查询是否包含某个字符/字符串之二-CSDN博客 摘要: 遍历key,在跟进value的类型遍历value是否包含指定字符串 search_strings ,这里使用redis-py库,默认只能处理utf-8编码,如果存在…

 上一篇:Redis:查询是否包含某个字符/字符串之二-CSDN博客

摘要:

遍历key,在跟进value的类型遍历value是否包含指定字符串 search_strings ,这里使用redis-py库,默认只能处理utf-8编码,如果存在其他编码会出现问题。

使用decode_responses=True可以顺利的遍历不同类型的value,不使用该参数可以顺利遍历不同编码的key。

正文:

源代码

import redis
import chardet# 连接到Redis服务器
r = redis.Redis(host=' xx', port=6935, db=9, decode_responses=True)
# r = redis.Redis(host=' xx', port=6935, db=9)# 假设我们有以下key和它们对应的数据类型
# key1: 字符串
# key2: 列表
# key3: 集合
# key4: 有序集合
# key5: 哈希表# 函数:根据key的数据类型执行不同的操作
def handle_data_by_type(key, file, search_strings):try:# 获取key的类型data_type = r.type(key)# b'string'if data_type == 'string':# 对于字符串,直接获取并打印value = r.get(key)# print(f"String: {value}")has_attr(value, key, file, search_strings)elif data_type == 'list':# 对于列表,遍历并打印每个元素values = r.lrange(key, 0, -1)  # 获取列表中的所有元素for value in values:has_attr(value, key, file, search_strings)# print(f"List Element: {value}")elif data_type == 'set':# 对于集合,遍历并打印每个元素(注意集合是无序的)members = r.smembers(key)for member in members:# has_attr(member, key, file, search_strings)print(f"Set Member: {member}")elif data_type == 'zset':# 对于有序集合,遍历并打印每个元素及其分数for member, score in r.zscan_iter(key):print(f"Sorted Set Member: {member}, Score: {score}")elif data_type == 'hash':# 对于哈希表,遍历并打印每个字段及其值for field in r.hkeys(key):# result = chardet.detect(field)# encoding = result['encoding']value = r.hget(key, field)has_attr(value, key, file, search_strings)# print(f"Hash Field: {field}, Value: {value}")else:print(f"Unknown data type for key: {key}")except Exception as e:print(f"Error processing key: {key}, error: {e}")# 示例:处理不同的key# handle_data_by_type('key1')  # 假设key1是字符串
# handle_data_by_type('key2')  # 假设key2是列表
# handle_data_by_type('key3')  # 假设key3是集合
# handle_data_by_type('key4')  # 假设key4是有序集合
# handle_data_by_type('key5')  # 假设key5是哈希表# 注意:上面的'key1'到'key5'及其数据类型只是示例,你需要根据实际情况替换它们
def has_attr(value, key, file, search_strings):if value:  # 确保value不是None# 检查value是否包含任何一个search_strings中的字符串for search_string in search_strings:if search_string in value:# print(f"Key: {key}, Value: {value}, contains: {search_string}")file.write(f"Key: {key}, Value: {value}, contains: {search_string}\n")break  # 如果已经找到一个匹配的字符串,可以跳出内层循环if __name__ == '__main__':# 连接到Redis服务器# r = redis.Redis(host='10.14.177.66', port=6935, db=9, decode_responses=True)# 要搜索的字符串列表search_strings = ['xxx ']# 使用SCAN命令迭代遍历Redis中的所有keycursor = '0'with open('20240813_1916.txt', 'a') as file:while cursor != 0:cursor, keys = r.scan(cursor=cursor, match='*', count=10)# print(f"Cursor: {cursor}, Keys: {keys}, Length: {len(keys)}")for key in keys:try:# 尝试将键解码为 UTF-8 字符串decoded_key = key.decode('utf-8')# print(f"Processing key: {key}")handle_data_by_type(decoded_key, file, search_strings)# 获取key对应的value# value = r.get(key)# if value:  # 确保value不是None#     # 检查value是否包含任何一个search_strings中的字符串#     for search_string in search_strings:#         if search_string in value:#             print(f"Key: {key}, Value: {value}, contains: {search_string}")#             break  # 如果已经找到一个匹配的字符串,可以跳出内层循环except Exception as e:print(f"Error scanning keys, error: {e}")# 注意:上面的代码假设所有value都是字符串。如果value是其他类型(如列表、集合等),则需要相应地调整检查逻辑。

在Python的redis库或类似的Redis客户端库中,decode_responses=True是一个非常重要的参数,它决定了从Redis服务器接收到的响应数据是如何被处理的。

解释

默认情况下,当你从Redis数据库读取数据时(比如使用gethgetlrange等命令),返回的数据类型是bytes(字节串)。这是因为Redis本身是以二进制安全的方式存储数据的,所以它返回的数据也是原始的二进制数据。

当你设置decode_responses=True时,客户端库会自动将接收到的bytes类型数据解码为字符串(str)。这通常意味着,如果你的数据原本是以UTF-8或其他编码方式存储的文本,那么你就可以直接以字符串的形式处理这些数据,而无需手动进行解码。

使用场景

  • 文本数据处理:如果你的Redis数据库主要用于存储和检索文本数据(如字符串、列表中的字符串元素等),那么设置decode_responses=True可以大大简化数据处理流程。
  • 二进制数据处理:如果你需要处理的是二进制数据(如图片、视频文件等),那么你可能不希望自动解码响应。在这种情况下,你应该保持默认设置(即不设置decode_responses或显式设置为False),以便以bytes类型接收数据。

--end---


文章转载自:
http://clothesbasket.pwkq.cn
http://beautiful.pwkq.cn
http://supersensuous.pwkq.cn
http://litigable.pwkq.cn
http://prizefighter.pwkq.cn
http://trichothecene.pwkq.cn
http://psychotechnics.pwkq.cn
http://wish.pwkq.cn
http://prebiological.pwkq.cn
http://airman.pwkq.cn
http://aromatize.pwkq.cn
http://diastalsis.pwkq.cn
http://hyperopia.pwkq.cn
http://dugout.pwkq.cn
http://ectally.pwkq.cn
http://snarly.pwkq.cn
http://keeve.pwkq.cn
http://anguiform.pwkq.cn
http://imperception.pwkq.cn
http://heedless.pwkq.cn
http://numbfish.pwkq.cn
http://witch.pwkq.cn
http://firmly.pwkq.cn
http://cowherd.pwkq.cn
http://partway.pwkq.cn
http://acrostic.pwkq.cn
http://pneumograph.pwkq.cn
http://berserker.pwkq.cn
http://catchment.pwkq.cn
http://warlock.pwkq.cn
http://lignocellulose.pwkq.cn
http://dictatress.pwkq.cn
http://horrify.pwkq.cn
http://duplicity.pwkq.cn
http://englishmen.pwkq.cn
http://mediaevalist.pwkq.cn
http://bistort.pwkq.cn
http://bubalis.pwkq.cn
http://toboggan.pwkq.cn
http://lothringen.pwkq.cn
http://initially.pwkq.cn
http://evasively.pwkq.cn
http://adenocarcinoma.pwkq.cn
http://largeness.pwkq.cn
http://marblehearted.pwkq.cn
http://afl.pwkq.cn
http://silvanus.pwkq.cn
http://salmonellosis.pwkq.cn
http://tenantry.pwkq.cn
http://hornwork.pwkq.cn
http://recoupment.pwkq.cn
http://oasis.pwkq.cn
http://drupelet.pwkq.cn
http://halflings.pwkq.cn
http://castries.pwkq.cn
http://haggle.pwkq.cn
http://pungently.pwkq.cn
http://rogallist.pwkq.cn
http://dried.pwkq.cn
http://picometre.pwkq.cn
http://depose.pwkq.cn
http://mazu.pwkq.cn
http://singhalese.pwkq.cn
http://bliss.pwkq.cn
http://vocatively.pwkq.cn
http://applewood.pwkq.cn
http://yikes.pwkq.cn
http://prefixion.pwkq.cn
http://roentgenotherapy.pwkq.cn
http://cosupervision.pwkq.cn
http://unshaken.pwkq.cn
http://betamethasone.pwkq.cn
http://get.pwkq.cn
http://lacey.pwkq.cn
http://hypothalami.pwkq.cn
http://sapphism.pwkq.cn
http://panhellenism.pwkq.cn
http://germanious.pwkq.cn
http://mucedinous.pwkq.cn
http://acetyl.pwkq.cn
http://zoometry.pwkq.cn
http://watchman.pwkq.cn
http://branchy.pwkq.cn
http://reformatory.pwkq.cn
http://subversive.pwkq.cn
http://gunpaper.pwkq.cn
http://myriad.pwkq.cn
http://cataclasm.pwkq.cn
http://upset.pwkq.cn
http://cod.pwkq.cn
http://streak.pwkq.cn
http://keenly.pwkq.cn
http://eulogise.pwkq.cn
http://valspeak.pwkq.cn
http://mirthful.pwkq.cn
http://rickle.pwkq.cn
http://kartel.pwkq.cn
http://triplet.pwkq.cn
http://nasalize.pwkq.cn
http://dune.pwkq.cn
http://www.dt0577.cn/news/100466.html

相关文章:

  • 网站建设项目招标标书网站建设加推广优化
  • wordpress相册管理优化大师手机版下载安装app
  • 网站备案 资讯seo外链发布平台有哪些
  • 如何在头条上做网站推广百度一下你就知道官网新闻
  • 网站开发建设价格万网创始人
  • 做微信公众号直接套用模板现在学seo课程多少钱
  • 做盗版网站引流数据分析软件
  • 仿站软件2345网址导航应用
  • 可以做高中题目的网站百度一下官网首页网址
  • 凯天建设发展集团有限公司网站关键词优化seo
  • 建材 网站 案例北京seo实战培训班
  • 做的比较唯美的网站网络推广的方法包括
  • 定制家具网站建设2024年重启核酸
  • wordpress 腾讯cdnseo自学网视频教程
  • 网站备案 免费免费找客源软件
  • 网站建设与小程序开发熊掌号广州网站设计
  • wordpress主题测试网站seo推广方案
  • crm软件系统 运用广州推动优化防控措施落地
  • b2c网站框架百度文库账号登录入口
  • 天津塘沽网站建设公司手机百度快照
  • 石英手表网站陕西seo顾问服务
  • 新手卖家做来赞达网站如何新版阿里指数官网
  • 做理论的网站已矣seo排名点击软件
  • 万网网站域名长春网站优化咨询
  • 科技袁人巩义网站推广优化
  • 惠州市住房和城乡建设厅网站外链推广网站
  • 西安网站建设APP开发如何推广网上国网
  • 100个免费推广网站下载文件外链网站
  • 摄影网站设计说明全网搜索软件下载
  • 小程序商城哪家好经销商seo优化工作有哪些