最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python導(dǎo)入導(dǎo)出redis數(shù)據(jù)的實(shí)現(xiàn)

 更新時(shí)間:2022年02月25日 14:12:50   作者:純潔的小魔鬼  
本文主要介紹了python導(dǎo)入導(dǎo)出redis數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

注:以String類型為例

一.導(dǎo)出redis某個(gè)庫的數(shù)據(jù)

import redis
import json

file_path = "why.json"

redis_conn = redis.Redis(host="192.168.1.123", port=6387, password="123zxcv", db=2, decode_responses=True)
data_keys = redis_conn.keys()

all_data = {}
for i in data_keys:
? ? all_data[i] = json.loads(redis_conn.get(i))


file_object = open(file_path, 'w', encoding="utf8")
json.dump(all_data, file_object, ensure_ascii=False)

file_object.close()

使用python向Redis批量導(dǎo)入數(shù)據(jù)

使用pipeline進(jìn)行批量導(dǎo)入數(shù)據(jù)。包含先使用rpush插入數(shù)據(jù),然后使用expire改動(dòng)過期時(shí)間

class Redis_Handler(Handler):
?? ?def connect(self):
?? ??? ?#print self.host,self.port,self.table
?? ??? ?self.conn = Connection(self.host,self.port,self.table)?? ?
?? ??? ?
?? ?def execute(self, action_name):
?? ??? ?filename = "/tmp/temp.txt"
?? ??? ?batch_size = 10000
?? ??? ?with open(filename) as file:
?? ??? ??? ?try:
?? ??? ??? ??? ?count = 0
?? ??? ??? ??? ?pipeline_redis = self.conn.client.pipeline()
?? ??? ??? ??? ?for lines in file:
?? ??? ??? ??? ??? ?(key,value) = lines.split(',')
?? ??? ??? ??? ??? ??? ?count = count + 1
?? ??? ??? ??? ??? ??? ?if len(key)>0:
?? ??? ??? ??? ??? ??? ??? ?pipeline_redis.rpush(key,value.strip())
?? ??? ??? ??? ??? ??? ??? ?if not count % batch_size:
?? ??? ??? ??? ??? ??? ??? ??? ?pipeline_redis.execute()
?? ??? ??? ??? ??? ??? ??? ??? ?count = 0
?? ??? ??? ?
?? ?
?? ??? ??? ??? ?#send the last batch
?? ??? ??? ??? ?pipeline_redis.execute()
?? ??? ??? ?except Exception:
?? ??? ??? ??? ?print 'redis add error'

二.導(dǎo)入redis某個(gè)庫的數(shù)據(jù)

import redis
import json

file_path = "why.json"

redis_conn = redis.Redis(host="192.168.1.123", port=6387, password="123zxcv", db=1, decode_responses=True)

file_object = open(file_path, 'r', encoding="utf8")
all_data = json.load(file_object)

for key in all_data:
? ? redis_conn.set(key, json.dumps(all_data[key], ensure_ascii=False))

file_object.close()

到此這篇關(guān)于python導(dǎo)入導(dǎo)出redis數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python導(dǎo)入導(dǎo)出redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 利用python中集合的唯一性實(shí)現(xiàn)去重

    利用python中集合的唯一性實(shí)現(xiàn)去重

    集合,用{ }表示,集合中所有元素具有唯一性。這篇文章給大家介紹利用python中集合的唯一性實(shí)現(xiàn)去重,感興趣的朋友一起看看吧
    2020-02-02
  • python計(jì)算日期之間的放假日期

    python計(jì)算日期之間的放假日期

    這篇文章主要為大家詳細(xì)介紹了python計(jì)算日期之間的放假日期,實(shí)現(xiàn)自動(dòng)查詢節(jié)日,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • scrapy與selenium結(jié)合爬取數(shù)據(jù)(爬取動(dòng)態(tài)網(wǎng)站)的示例代碼

    scrapy與selenium結(jié)合爬取數(shù)據(jù)(爬取動(dòng)態(tài)網(wǎng)站)的示例代碼

    這篇文章主要介紹了scrapy與selenium結(jié)合爬取數(shù)據(jù)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Python實(shí)現(xiàn)的矩陣轉(zhuǎn)置與矩陣相乘運(yùn)算示例

    Python實(shí)現(xiàn)的矩陣轉(zhuǎn)置與矩陣相乘運(yùn)算示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的矩陣轉(zhuǎn)置與矩陣相乘運(yùn)算,結(jié)合實(shí)例形式分析了Python針對矩陣進(jìn)行轉(zhuǎn)置與相乘運(yùn)算的相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2019-03-03
  • Python flask項(xiàng)目入門教程

    Python flask項(xiàng)目入門教程

    flask 是一門使用 python 編寫的后端框架,這篇文章主要介紹了Python flask項(xiàng)目入門教程,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • 詳解python數(shù)據(jù)結(jié)構(gòu)之棧stack

    詳解python數(shù)據(jù)結(jié)構(gòu)之棧stack

    這篇文章主要介紹了詳解python數(shù)據(jù)結(jié)構(gòu)之棧stack,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • 最新評論

    张北县| 藁城市| 万源市| 栾川县| 普兰店市| 翁牛特旗| 广西| 宁国市| 兴化市| 佛冈县| 灌云县| 海安县| 高邮市| 连城县| 托克逊县| 栾川县| 巫山县| 林州市| 台东县| 喀喇| 卢龙县| 怀仁县| 泰顺县| 克什克腾旗| 东源县| 泰兴市| 陆良县| 沙坪坝区| 临武县| 乐山市| 渝北区| 垣曲县| 卢氏县| 高要市| 合作市| 泸州市| 育儿| 乌兰察布市| 张家港市| 全州县| 额尔古纳市|