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" ?

Selman
  • 274
  • 2
  • 4
  • 17
  • Maybe help you , you see this ? [https://stackoverflow.com/questions/30658193/python3-how-to-make-a-bytes-object-from-a-list-of-integers](https://stackoverflow.com/questions/30658193/python3-how-to-make-a-bytes-object-from-a-list-of-integers) – batuhanilgarr Jul 18 '22 at 09:44

3 Answers3

1
import json

json.loads(b'["ADA/USD","ADA/USDT","ALGO/USD","ATOM/USD"]'.decode())
Devyl
  • 565
  • 3
  • 8
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'>
Mortz
  • 4,654
  • 1
  • 19
  • 35
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('/')

The Thonnu
  • 3,578
  • 2
  • 8
  • 30