0

I am in the process of migrating one of my code repo to Python 3.x. I noticed that the redis.StrictRedis class is converting String by string dict to bytes by bytes dict.

# Input
{ u'key': u'value'}

# output
{b'key': b'value'}

Currently, I am fixing this by having a utility method which converts those bytes to string.

def convert_binary_dict_items(values):
    # type: (Dict[Any, Any]) -> Dict[Any, Any]
    """
    This method convert dictionary items which are of the binary type to string type.

    :param values: Dictionary object.
    :return:
    """

    result = dict()

    if not values:
        return result

    for key, value in six.iteritems(values):

        modified_key = key.decode(u'utf-8') if type(key) == bytes else key
        modified_value = value.decode(u'utf-8') if type(key) == bytes else value

        result[modified_key] = modified_value

    return result

Is there a smart solution available? did anyone run into the similar issue?

Rakesh
  • 3,987
  • 10
  • 43
  • 68
  • What does your code look like? How are you storing (and reading) Python dictionaries to redis? – larsks Jan 08 '18 at 19:27
  • 1
    My initial thought is that this is a duplicate of https://stackoverflow.com/questions/25745053/about-char-b-prefix-in-python3-4-1-client-connect-to-redis and all you need to do is configure the client appropriately. – Tague Griffith Jan 08 '18 at 23:19
  • 1
    @TagueGriffith you are right it was configuration issue. I was able to solve it by setting `decode_responses` flag. – Rakesh Jan 09 '18 at 05:37
  • Possible duplicate of [About char b prefix in Python3.4.1 client connect to redis](https://stackoverflow.com/questions/25745053/about-char-b-prefix-in-python3-4-1-client-connect-to-redis) – Tague Griffith Jan 10 '18 at 07:04

0 Answers0