0

I am using redis.get method and it's returns me byte.

In redis ı have a list like that:

[
    "ADA/USD",
    "ADA/USDT",
    "ALGO/USD",
    "ATOM/USD"
]

When get this list inside of my script with redis.get

It's return me

b'["ADA/USD","ADA/USDT","ALGO/USD","ATOM/USD"]'

How can I convert to a "Byte" to "List" ?

1

3 Answers 3

1
import json

json.loads(b'["ADA/USD","ADA/USDT","ALGO/USD","ATOM/USD"]'.decode())
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to convert the bytes to a list - you could convert it to a string object and then split (after replacement) -

x = b'["ADA/USD","ADA/USDT","ALGO/USD","ATOM/USD"]'.decode().replace('[', '').replace(']', '').split(',')

Output

# print(x)
['"ADA/USD"', '"ADA/USDT"', '"ALGO/USD"', '"ATOM/USD"']
# print(type(x))
<class 'list'>

Comments

0
x = b'["ADA/USD","ADA/USDT","ALGO/USD","ATOM/USD"]'
my_list = eval(x)
print(my_list) # ['ADA/USD', 'ADA/USDT', 'ALGO/USD', 'ATOM/USD']
print(type(my_list)) # <class 'list'>

Note: this could be dangerous if you're accepting a user input and then evaluating: check first that x is not something like __import__('shutil').rmtree('/')

Comments

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.