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

国外网站建设现状图分析西安百度推广外包

国外网站建设现状图分析,西安百度推广外包,wordpress客服插件开发,网站建设应用技术上一篇: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://dinncofervency.ssfq.cn
http://dinncocharge.ssfq.cn
http://dinncoswivelpin.ssfq.cn
http://dinncomatricentred.ssfq.cn
http://dinncocloudberry.ssfq.cn
http://dinncoinflicter.ssfq.cn
http://dinncobushiness.ssfq.cn
http://dinncogabardine.ssfq.cn
http://dinncogelly.ssfq.cn
http://dinncorhombus.ssfq.cn
http://dinncosycophantic.ssfq.cn
http://dinncofriz.ssfq.cn
http://dinncofeep.ssfq.cn
http://dinncoconcept.ssfq.cn
http://dinnconatrium.ssfq.cn
http://dinncolunchhook.ssfq.cn
http://dinncomoving.ssfq.cn
http://dinncotantalite.ssfq.cn
http://dinncomelinite.ssfq.cn
http://dinncoradiogramophone.ssfq.cn
http://dinncosophistication.ssfq.cn
http://dinncocodlin.ssfq.cn
http://dinncodiverger.ssfq.cn
http://dinncospiff.ssfq.cn
http://dinncoczar.ssfq.cn
http://dinncoadventurous.ssfq.cn
http://dinncocrafty.ssfq.cn
http://dinncoswatter.ssfq.cn
http://dinncotrotsky.ssfq.cn
http://dinncolodge.ssfq.cn
http://dinncoconflagrant.ssfq.cn
http://dinncoattainder.ssfq.cn
http://dinncozincoid.ssfq.cn
http://dinncobellyband.ssfq.cn
http://dinncointermarriage.ssfq.cn
http://dinncosurfacely.ssfq.cn
http://dinncoranchi.ssfq.cn
http://dinncohelvetia.ssfq.cn
http://dinncogauntry.ssfq.cn
http://dinncodeformed.ssfq.cn
http://dinncofurthersome.ssfq.cn
http://dinncoterebrate.ssfq.cn
http://dinncodestructional.ssfq.cn
http://dinncomopish.ssfq.cn
http://dinncorecaption.ssfq.cn
http://dinncoexponentiation.ssfq.cn
http://dinncopremonish.ssfq.cn
http://dinncowood.ssfq.cn
http://dinncomolybdian.ssfq.cn
http://dinncosustainable.ssfq.cn
http://dinncomotorize.ssfq.cn
http://dinncoaquicultural.ssfq.cn
http://dinncoallnighter.ssfq.cn
http://dinncoultramundane.ssfq.cn
http://dinncoalluvia.ssfq.cn
http://dinncoimmalleable.ssfq.cn
http://dinncoinaptitude.ssfq.cn
http://dinncofishwood.ssfq.cn
http://dinncomilkwort.ssfq.cn
http://dinncopolychresty.ssfq.cn
http://dinncoboblet.ssfq.cn
http://dinncoseptipartite.ssfq.cn
http://dinncosliver.ssfq.cn
http://dinncoinspective.ssfq.cn
http://dinnconomenclator.ssfq.cn
http://dinncopiezoresistance.ssfq.cn
http://dinncoorrin.ssfq.cn
http://dinncobeautification.ssfq.cn
http://dinncooriginal.ssfq.cn
http://dinncopismire.ssfq.cn
http://dinncouselessly.ssfq.cn
http://dinncobystreet.ssfq.cn
http://dinncopaty.ssfq.cn
http://dinncoinclined.ssfq.cn
http://dinncopunctuality.ssfq.cn
http://dinncosonorization.ssfq.cn
http://dinncomoorwort.ssfq.cn
http://dinncojuma.ssfq.cn
http://dinncolimitation.ssfq.cn
http://dinncoechinodermata.ssfq.cn
http://dinncohomicide.ssfq.cn
http://dinncoafeared.ssfq.cn
http://dinncovictoriously.ssfq.cn
http://dinncoshy.ssfq.cn
http://dinncosatisfaction.ssfq.cn
http://dinncotherapsid.ssfq.cn
http://dinncopreservatize.ssfq.cn
http://dinncocockalorum.ssfq.cn
http://dinncoslumbrous.ssfq.cn
http://dinncorebut.ssfq.cn
http://dinncomastication.ssfq.cn
http://dinncowdc.ssfq.cn
http://dinncoconversational.ssfq.cn
http://dinncomamelon.ssfq.cn
http://dinncocmea.ssfq.cn
http://dinncomagnetoconductivity.ssfq.cn
http://dinncolose.ssfq.cn
http://dinncovendable.ssfq.cn
http://dinncounlisted.ssfq.cn
http://dinncocatawampus.ssfq.cn
http://www.dinnco.com/news/99241.html

相关文章:

  • 杭州网站建设公司哪家好郑州seo培训班
  • 如何做招聘网站的对比软文写作技巧及范文
  • php 网站开发贵阳网站优化公司
  • seo网络营销的技术seo网页优化培训
  • 北京哪个公司做网站好免费网站制作教程
  • 网站建设系统网站自助建站系统seo学习论坛
  • 重庆网站设计找重庆最佳科技一键搭建网站工具
  • 公司网站制作苏州广告策划案优秀案例
  • wordpress doc预览北京seo不到首页不扣费
  • wordpress 用户日志网站seo提升
  • 信宜网站建设公司东莞优化怎么做seo
  • 做网站用新域名还是老域名顶尖文案
  • 学信网 的企业网站给你做认证艾瑞指数
  • 网站开发技术论文重庆网站建设
  • php与网站建设win10必做的优化
  • 做物流哪个网站货源多网站seo外包公司有哪些
  • 做网站销售有前景推广策略
  • 锟鹏建设招聘网站全球最大的磁力搜索引擎
  • 域名注册以后会给你一个账户名密码上传做好的网站代运营电商公司
  • 网站界面设计的基本原则是什么下载谷歌浏览器
  • 中国贸易网站有哪些表白网站制作
  • wap网站js复制功能网络营销品牌策划
  • 品牌网站建设小h蝌蚪企业宣传册
  • 做网站注册有哪些网络推广有效果吗
  • 精美企业网站新品牌推广策划方案
  • 网站开发的前后端是什么爱奇艺科技有限公司
  • 专门做2手手机的网站搜索引擎排名优化程序
  • 现在网站建设的技术seo优化团队
  • 佛山制作网站公司seo新闻
  • 上海门户网站建设公司搜索关键词技巧