簡體   English   中英

如何使用for循環使用python腳本將值1-100插入redis?

[英]How to insert values 1-100 into redis with python script using for loop?

我試圖通過Python腳本將100個值插入Redis,數據類型無關緊要。

我嘗試過使用列表,但是除了手動執行操作之外,如何增加列表中的值(不接受字符串)。 例如:

r.lpush list 1    
r.lpush list 2   
etc.

我不想輸入100個lpush 如何做到這一點?

我已經嘗試過使用字符串並增加字符串,但是我必須不斷更改值。 例如:

set key 1   
set key2 2  
set key3 3

那么,世界上如何才能將值1-100插入到redis中以便可以讀取它們呢?

您是否嘗試過類似的方法:

import redis

r = redis.Redis( url='rediss://:password@hostname:port/0',
    password='password',
    ssl_keyfile='path_to_keyfile',
    ssl_certfile='path_to_certfile',
    ssl_cert_reqs='required',
    ssl_ca_certs='path_to_ca_certfile')

for in range(1,100):
    r.set('foo{}'.format(i), 'bar{}'.format(i))
for i in range(1, 101):
    r.lpush("list", str(i))

range(1, 101)生成從1 (包括)到101 (不包括)的整數, str(i)i (例如57 )更改為字符串(例如"57" )。

最終得到以下python代碼,將值0-100輸入到Redis中

i = 1
while(i < 101):
    r.rpush('value', i)
    i += 1

#Print out the values from 1 - 100  
print(r.lrange('value', 0, -1))

感謝大家!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM