5

I would like to store all my string using Utf8 String Codec with GZIP in python.

I tried below code but compression not happening properly. I don't know what's missing here. How to insert data into redis using gzip compression technique.

After insertion into redis it just printing some number like d49

import redis
import StringIO
import gzip

r = redis.StrictRedis(host='127.0.0.1', port=80, db=0, decode_responses=True)

out = StringIO.StringIO()
with gzip.GzipFile(fileobj=out, mode='w') as f:
    value = f.write('this is my test value')
    r.set('test', value)

Appreciated your help in advance!

Thanks

1 Answer 1

3

You can directly use compress() method of gzip.

If you are directly compressing the text string,

import redis
import gzip

r = redis.StrictRedis(host='127.0.0.1', port=80, db=0, decode_responses=True)
text = 'this is my test value'
value = gzip.compress(text.encode())
r.set('test', value)

If you are trying to compress the file,

import redis
import gzip

r = redis.StrictRedis(host='127.0.0.1', port=80, db=0, decode_responses=True)
with open('text.txt', 'rb') as fp:
    value = gzip.compress(fp.read())
    r.set('test', value)
Sign up to request clarification or add additional context in comments.

1 Comment

Which python version you are using? It's working in python 3.5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.