# Casting in Python and Redis

DevFeed: [Casting in Python and Redis](<https://devfeed.tech/articles/casting-in-python-and-redis-28232.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/python/2020/03/08/casting-in-python-and-redis.html>)

Author: Fuzzygroup

Published: 2020-03-08T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [Redis](<https://devfeed.tech/topics/redis.md>), [exceptions](<https://devfeed.tech/topics/exceptions.md>), [debugging](<https://devfeed.tech/topics/debugging.md>)

Tags: [debugging](<https://devfeed.tech/tags/debugging.md>), [exceptions](<https://devfeed.tech/tags/exceptions.md>), [filesystem](<https://devfeed.tech/tags/filesystem.md>), [import](<https://devfeed.tech/tags/import.md>), [linux](<https://devfeed.tech/tags/linux.md>), [python](<https://devfeed.tech/tags/python.md>), [redis](<https://devfeed.tech/tags/redis.md>)

## AI overview

A debugging account of a Python and Redis WRONGTYPE error caused by byte-string conversion. The author reports that wrapping Redis list names and values with str() resolved the operation, and that decoding byte ranges with UTF-8 was relevant to the string-conversion issue.

## Source excerpt

I recently had this situation in Python and Redis: redis.lpush(kafka_topic_name, current_filename) redis.exceptions.ResponseError: WRONGTYPE Operation against a key holding the wrong kind of value So I did my normal deal of debugging by adding: import pdb and then a: pdb.set_trace() before the lpush call and I tried it manually by typing out redis.lpush(kafka_topic_name, current_filename) ENTER and it, naturally, worked fine. Grumble. What I finally ended up doing is wrapping a str() call as follows: redis.lpush(str(kafka_topic_name), (current_filename)) And that let things work correctly. In talking with another Python programmer, we came to the conclusion that Python's auto casting of things between byte literals and strings was the culprit. Wrapping a str() call around both the list name and the list value solved things nicely. Note: I figured this out by moving from a lpush operation on Redis to a sadd operation and then retrieving back a single entry which showed me the Python b prefix: b'/Users/sjohnson/Sync/fuzzygroup/adl/ohi_kafka/experts-all/data/twitter/25400bcd1142564b4476c178935bd736.json' Also - Don't Forget About Unicode I dug into this further and a least part of this issue for string conversion from Python byte ranges into actual strings is that you need to use a current_filename.decode("utf-8") call because the Python byte range has no idea what the encoding was. Now I will admit that I'm surprised that Python didn't realize "Ok we're reading data from the filesystem's dir() operation" and it is OS-X so the encoding must be XYZ (utf-8 I would think). Here's an example of this: >>> import sys >>> sys.stdout.encoding 'UTF-8' And since my coworker on this project uses Linux, I guess I need to embed that into my application (note - it isn't 100% clear to me that sys.stdout.encoding is the same as the encoding of the filesystem's directory output; there is also the sys.setdefaultencoding('utf-8') call). See Also: Stack Overflow 1 Stack Overflow 2 Related