LoginSignup
0
0

More than 1 year has passed since last update.

python redisで全データ表示するやーつ

Posted at
from pprint import pprint
import redis
red = redis.Redis(*redis_host, db=db_num)

# 全データの表示
all_keys = {k.decode(): red.type(k).decode() for k in red.keys()}
for k, t in all_keys.items():
    print(f'key: {k}, type: {t}')
    if t == 'string':
        print(red.get(k).decode())
    if t == 'list':
        pprint([m.decode() for m in red.lrange(k, 0, -1)])
    if t == 'set':
        pprint([m.decode() for m in red.smembers(k)])
    if t == 'zset':
        pprint([(m.decode(), red.zrank(k, m), red.zscore(k, m)) for m in red.zrange(k, 0, -1)])
    if t == 'hash':
        pprint([(f.decode(), v.decode()) for f, v in red.hgetall(k).items()])

    print()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0