python redis增删改查

这只是我用到的redis数据类型

import redis
r = redis.StrictRedis(host='127.0.0.1', password='123456', port=6379, db=3, decode_responses=True)

'''
# from https://www.runoob.com/w3cnote/python-redis-intro.html
# 集合
r.sadd(name, values)  # 添加
r.srem(name, values)  # 删除
r.smembers(name)  # 查询所有

# 有序集合
r.zadd(name, {value: score})  # 添加
r.zrem(name, values)  # 删除
result = r.zrevrange(name, 0, -1, withscores=True) # 获取有序集合中所有元素和分数,分数倒序
result.reverse()  # 使原列表倒序,原列表是按分数倒序,则reverse()后原列表变成按分数升序

# 字符串,同一个name只能有一个value
r.set(name, value)  # 添加
r.delete(name)  # 删除
r.get(name)  # 查询

命令行
若redis是手动编译安装的,且未启动redis,则搜索redis.conf的路径,将"daemonize no"修改为"daemonize yes",进入/usr/local/bin,执行redis-server redis.conf的路径,启动redis。from https://cloud.tencent.com/developer/article/1700611
redis-cli -h host -p port -a password --raw  # from https://www.runoob.com/redis/redis-commands.html
keys *  # 返回这个数据库中所有的键名  # 这个及下面两个from https://www.runoob.com/redis/redis-keys.html
type key_name  # 查询这个键的值的数据类型
del key_name  # 删除这个数据库中的键是key_name的整个键值对
'''

name_1 = 'test_1_set'
name_2 = 'test_2_youxu_set'
name_3 = 'test_3_string'

def test_add():
    r.sadd(name_1, name_1)
    r.zadd(name_2, {name_2:'123'})
    r.set(name_3, name_3)

def test_cha():
    print(r.smembers(name_1))
    print(r.zrevrange(name_2, 0, -1, withscores=True))
    print(r.get(name_3))

def test_del():
    print(r.srem(name_1, name_1))
    print(r.zrem(name_2, name_2))
    print(r.delete(name_3))

# test_add()
# test_cha()
# test_del()

# print(r.sadd('abc','456'))
result = r.smembers('abc')
print(result)
print(type(result))
for i in result:
    print(r.srem('abc', i))

你可能感兴趣的:(python爬虫学习,python,redis)